将文本字段数据保存到数据库的代码

时间:2013-02-08 14:58:03

标签: c# asp.net asp.net-mvc

我在编写用于将文本字段框信息保存到数据库中的代码时遇到问题,这就是我所拥有的 - 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);
}

2 个答案:

答案 0 :(得分: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);



}

答案 1 :(得分:1)