Skip to main content
 首页 » 编程设计

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

2024年02月20日15findumars

我在编写用于将文本字段框信息保存到数据库中的代码时遇到问题,这就是我所拥有的 - 获取和发布方法:

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

请您参考如下方法:

您不需要在此处使用 FormCollection,因为它看起来就像您正在绑定(bind)到一个模型。

在 View 中你应该有

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