2013-06-20 186 views
-2

我想插入值到數據庫。控制器操作

我的模型是

public bool Create(string userName) { 
     _userNA = userName; 
     if (validate()) { 
      using (DatabaseCommaned cmd = new DatabaseCommaned()) { 
       cmd.CommandText = "INSERT INTO tUser(UNA, FNAID, MNAID, LNAID, Email, MobCountryCode, Mobile, ST, LN, ExpDT) VALUES(@una, @fnaid, @mnaid, @lnaid, @email, @mobCode, @mobile, @st, @ln, @exp)"; 
       cmd.Parameters.AddWithValue("@una", userName); 
       .... 


       _userID = cmd.ExecuteInsertAndGetID(); 
       if (_userID > 0) { 
        copyPropertiesInternally(); 
        Logs.LogEvent(LogEvent.UserCreated, userName); 
        ResetPassword(); 
        return true; 
       } 
       else { 
        _userNA = string.Empty; 
        return false; 
       } 
      } 
     } 
     else 
      return false; 
    } 

有誰能夠告訴如何控制器被寫在上面的模型

+3

你的命名約定吸.. –

+0

@BhushanFirake遺憾。你可以告訴我從控制器通過哪些值 – user2495593

+3

它很不清楚你在問什麼.. –

回答

1

不是100%肯定你問什麼,但我會在它採取平底船:

public ActionResult Create(string UserName) 
{ 
    var model = new User(); 
    var created = model.Create(UserName); 
    if(created) 
     return View(); 
    else 
     return RedirectToAction("Index"); 
} 

編輯在迴應評論

你說你有一個模型。它應該是這個樣子:

public class UserModel 
{ 
    public string UserName {get;set} 
    public string Email {get;set} 
    public string FirstName {get;set} 
} 

你的控制器行動,那麼應該是:

public ActionResult Create(UserModel model) 
{ 
    if (validate(model) { 
     var user = new User(); 
     user.Create(model); 
     return View(); 
    } 
    else 
     return RedirectToAction("Index"); 
} 

您的數據庫插入的代碼應該是:

public void Create(UserModel model) { 
     _userNA = model.UserName; 
     using (DatabaseCommaned cmd = new DatabaseCommaned()) { 
     cmd.CommandText = "INSERT INTO tUser(UNA, FNAID, MNAID, LNAID, Email, MobCountryCode, Mobile, ST, LN, ExpDT) VALUES(@una, @fnaid, @mnaid, @lnaid, @email, @mobCode, @mobile, @st, @ln, @exp)"; 
     cmd.Parameters.AddWithValue("@una", userName); 
     //.... 


     _userID = cmd.ExecuteInsertAndGetID(); 
     if (_userID > 0) { 
       copyPropertiesInternally(); 
       Logs.LogEvent(LogEvent.UserCreated, userName); 
       ResetPassword(); 
       return true; 
     } 
     else { 
       _userNA = string.Empty; 
       return false; 
     } 
    } 
} 

這是一個指針只能從破碎您提供的信息位。它會希望把你放在正確的道路上。

+0

對不起。這是不working.it重定向到索引page.not插入分貝 – user2495593

+0

這意味着你的數據庫插入是錯誤的,而不是控制器的動作。什麼是你的validate()方法有什麼作用? – jzm

+0

bool validate(){ return!string.IsNullOrEmpty(UserName)&&!string.IsNullOrEmpty(Email)&&!string.IsNullOrEmpty(FirstName); } – user2495593

1

創建一個ViewModel類,例如CreateUserViewModel

public class CreateUserViewModel 
{ 
    //add properties reflecting the fields you want to capture in your HTML form 

    [Required] 
    public string UserName { get; set; } 

    //... etc 
} 

然後創建您的控制器

public ActionResult CreateUser(CreateUserViewModel model) 
{ 
    if(ModelState.IsValid) 
    { 
     string username = model.UserName; 
     //... etc 

     bool created = MyUserModelClass.Create(username, ...); 
     if(created) 
      Return RedirectToAction("Index"); 
     else 
      return View(model); //return the form 
    } 
    else 
    { 
     return View(model); //return the form 
    } 
} 
+0

謝謝u.let我試試 – user2495593

+0

對不起,我被創建是假的 – user2495593