2013-02-08 106 views
1

我无法写代码保存的文本字段中输入信息到数据库中,这就是我 - 一个Get和Post方法:代码以文本字段的数据保存到数据库

public ActionResult _UserName(UserNameModel userNameModel) 
{ 
    using (var db = new DataConnection()) 
    { 
     userNameModel.UserName= (from u in db.Users 
           where u.ID == WebSecurity.CurrentUserId 
           select u.UserName).FirstOrDefault(); 
     } 
     return View(userNameModel); 
    } 
} 


[HttpPost] 
public ActionResult _CreateUserName(string username, UserNameModel userNameModel) 
{ 
    if (ModelState.IsValid) 
    { 
     using (var db = new DataConnection()) 
     { 
      try 
      { 
       // Need to save what has been entered in the textbox 
       // to the Users table in the database. 
      } 
      catch (Exception) 
      { 
       throw; 
      } 
     } 
     return RedirectToAction("UserNamePage"); 
    } 
    return View(userNameModel); 
} 

回答

3

这里您不需要使用FormCollection,因为它看起来就像绑定到模型一样。

在视图,你应该有

@model MySystem.Models.UserNameModel 

@Html.TextBoxFor(m=> m.Username) 

然后当你提交表单你可以从userNameModel.Username;

你的Post方法得到的模型数据只需要收集模型数据

[HttpPost] 
public ActionResult _CreateUserName(UserNameModel userNameModel) 
{ 
    if (ModelState.IsValid) 
    { 
     using (var db = new DataConnection()) 
     { 
     try 
     { 
      db.UsersTable.InsertOnSubmit(new User(){ Username = userNameModel}); 
      // Need to save what has been entered in the textbox 
      // to the Users table in the database. 
     } 
     catch (Exception) 
     { 
      throw; 
     } 
    } 
    return RedirectToAction("UserNamePage"); 
} 
return View(userNameModel); 



} 

编辑我与数据库交互的首选方法是在您的项目中使用Linq添加一个新项目>数据> LinqToSql。这将给你一个图解概述,你可以简单地将表和过程拖到屏幕上。这将创建每个表格的模型。

然后,您可以创建一个Linq上下文的实例并使用它来更新数据库,您将看到intellisense会在您的设计器中选取表格。然后,您就可以与数据库非常干净

[HttpPost] 
public ActionResult _CreateUserName(UserNameModel userNameModel) 
{ 
    if (ModelState.IsValid) 
    { 

     try 
     { 
      myLinqDataContext db = new myLinqDatacontext(); // in your web config amend the connection string this created 

      db.UsersTable.InsertOnSubmit(new User(){ Username = userNameModel}); 
      // Need to save what has been entered in the textbox 
      // to the Users table in the database. 
     } 
     catch (Exception) 
     { 
      throw; 
     } 

    return RedirectToAction("UserNamePage"); 
} 
return View(userNameModel); 



} 

如果你想更新你将需要调用记录的一个实例数据库

[HttpPost] 
public ActionResult _CreateUserName(UserNameModel userNameModel) 
{ 
    if (ModelState.IsValid) 
    { 

     try 
     { 
      myLinqDataContext db = new myLinqDatacontext(); // in your web config amend the connection string this created 

      UserTable user = db.UsersTable.Where(m=> m.Username == userNameModel.UserName).FirstOrDefault(); 

      // apply new information 
      user.username = userNameModel.UserName; 

      // commit the changes 
      db.SubmitChanges(); 

      // Need to save what has been entered in the textbox 
      // to the Users table in the database. 
     } 
     catch (Exception) 
     { 
      throw; 
     } 

    return RedirectToAction("UserNamePage"); 
} 
return View(userNameModel); 



} 
+0

+1我在想同样的交互东西 – 2013-02-08 15:27:40

+0

当我在提交语句插入时键入 - 它被加下划线和错误味精是:参数类型'ProjectName.Models.User'不可分配给参数类型'Data.SQL.User'这是什么意思? – 072et 2013-02-08 15:28:16

+0

如果我想更新而不是插入,看起来会怎么样 – 072et 2013-02-08 16:25:57