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);
}
4

2 回答 2

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,在您的项目中添加一个新项目 > Data > LinqToSql。这将为您提供图表概览,您可以简单地将表格和过程拖到屏幕上。这将创建每个表的模型。

然后,您可以创建 Linq 上下文的实例并使用它来更新您的数据库,您将看到智能感知将在您的设计器中获取表。然后,您将能够非常干净地与数据库进行交互

[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);



}
于 2013-02-08T15:06:56.403 回答
1

看看这个:http ://stack247.wordpress.com/2011/03/20/get-forms-post-values-in-asp-net-mvc-with-formcollection/

这应该做你需要的。

于 2013-02-08T15:01:20.243 回答