2013-08-21 58 views
1

我米使用控制器插入數據,插入數據

SignUpcontroller.cs

[HttpPost] 
public ActionResult Index(SignUpModel sm) 
{ 

using(DataClassesDataContext dc= new DataClassesDataContext()) 
{ 
Dummytable dm= new Dummytable(); 
{ 
dm.Name=sm.password; 
} 
//then conncetion string and submit 
} 
} 

和重定向

我的問題是,它是正確的寫控制器模塊在此代碼或者我需要把它寫在模型模塊,如果我需要把它寫在模型模塊那麼如何定義的setter幫助我

回答

0

的實施工作,我JST給類的內存從控制器

[HttpPost] 
public ActionResult Index(SignUpModel sm) 
{ 
ISignUpModel ISign= (ISignUpModel)this.sm; 
ISign.Insert(sm); 
} 

感謝所有的人鑄造的幫助界面,因爲你我的全部學習這個:)

和在SignUpModel.cs,是signupmodel.cs實施

3

它在數據訪問層中移動所有數據訪問代碼是更好的做法。所以簡單地把這個代碼放在一個單獨的類中,你可以從你的控制器中引用和調用。例如,你可以定義將定義不同的操作界面:

public interface IRepository 
{ 
    void Insert(SignUpModel model); 
} 

,然後有一個正在與你正在使用(EF爲例)的數據訪問技術的具體實施:

public class RepositoryEF : IRepository 
{ 
    public void Insert(SignUpModel model) 
    { 
     using(DataClassesDataContext dc= new DataClassesDataContext()) 
     { 
      Dummytable dm = new Dummytable(); 
      dm.Name = sm.password; 
     } 
    } 
} 

,下一步是讓你的控制器利用這個資源庫作爲構造依賴性:現在所有剩下的就是拿起一些DI框架和線

public class SomeController : Controller 
{ 
    private readonly IRepository repo; 
    public SomeController(IRepository repo) 
    { 
     this.repo = repo; 
    } 

    [HttpPost] 
    public ActionResult Index(SignUpModel sm) 
    { 
     this.repo.Insert(sm); 

     ... 
    } 
} 

依賴關係。

這樣,你有你的控制器邏輯和數據訪問層之間的明確分工。這將允許您分離測試應用程序的各個層次。

+0

在那裏我已經寫了吸氣二傳手正常「的命名爲ISignUp用插入法界面」?是否可以在該模塊中包含更多課程? – Debasish

+0

您可以在單獨的.cs文件中定義接口和實現。 –

+0

是界面是強制需要的嗎? – Debasish

0

第一個問題是哪裏是你的DataAccessLayer

所以更好的做法是在寫讀另一個類的代碼和寫數據庫值。

controller只對UI邏輯

,你可以使用Interface爲增加可重用性原因和單元測試。

enter image description here

+0

我已經使用DataClasses.dbml – Debasish

0

這是不常見的用於重要數據存儲/訪問等註冊。考慮網絡安全工具,不要直接處理這種類型的數據。或者改變你的意思來處理普通數據。

+0

我新與MVC我只是想知道我應該把那個代碼放在哪裏? – Debasish