ASP.NETMVC中更新数据表中当前行并添加新记录的方法。
创始人
2024-09-19 09:01:14
0
  1. 首先,在Model中定义数据表对应的类。例如,数据表为Student,那么可以定义如下的类:
public class Student
{
    public int Id { get; set; }
    public string Name { get; set; }
    public int Age { get; set; }
    // 其他属性
}
  1. 在Controller中定义更新当前行的方法和添加新记录的方法:
// 更新当前行
[HttpPost]
public ActionResult UpdateStudent(Student student)
{
    using (var db = new DbContext())
    {
        db.Entry(student).State = EntityState.Modified;
        db.SaveChanges();
    }
    return RedirectToAction("List");
}

// 添加新记录
[HttpPost]
public ActionResult AddStudent(Student student)
{
    using (var db = new DbContext())
    {
        db.Entry(student).State = EntityState.Added;
        db.SaveChanges();
    }
    return RedirectToAction("List");
}
  1. 在View中,可以通过表单提交来调用更新当前行的方法和添加新记录的方法:
@model Student
@using (Html.BeginForm("UpdateStudent", "Student", FormMethod.Post))
{
    @Html.HiddenFor(model => model.Id)
    @Html.TextBoxFor(model => model.Name)
    @Html.TextBoxFor(model => model.Age)

    
}
@using (Html.BeginForm("AddStudent", "Student", FormMethod.Post))
{
    @Html.TextBoxFor(model => model.Name)
    @Html.TextBoxFor(model => model.Age)

    
}

通过表单提交,可以将输入的数据封装成Student对象传递给Controller中的方法,从而更新当前行或者添加新记录。

相关内容

热门资讯

透视有挂!xpoker辅助器(... 透视有挂!xpoker辅助器(透视)新2025版(2020已更新)(哔哩哔哩)1、xpoker辅助器...
透视模拟器!aapoker透视... 透视模拟器!aapoker透视脚本(透视)发牌逻辑(总是有挂);1、任何aapoker透视脚本ai辅...
透视免费!聚星ai辅助工具收费... 透视免费!聚星ai辅助工具收费多少(透视)科技教程(2020已更新)(哔哩哔哩);1、玩家可以在聚星...
透视软件!aapoker插件下... 透视软件!aapoker插件下载(透视)插件下载(都是真的有挂);1、下载好aapoker插件下载辅...
透视ai代打!智星德州插件(透... 透视ai代打!智星德州插件(透视)解密教程(2025已更新)(哔哩哔哩);小薇(透视辅助)致您一封信...
透视总结!aapoker能控制... 透视总结!aapoker能控制牌吗(透视)插件(切实有挂)1、操作简单,无需注册,只需要使用手机进行...
透视数据!竞技联盟辅助(透视)... 透视数据!竞技联盟辅助(透视)教你攻略(2021已更新)(哔哩哔哩)亲,关键说明,竞技联盟辅助赛季回...
辅助透视!aapoker公共底... 辅助透视!aapoker公共底牌(透视)辅助(好像存在有挂)1、让任何用户在无需aapoker公共底...
透视有挂!佛手在线大菠萝辅助(... 透视有挂!佛手在线大菠萝辅助(透视)科技教程(2026已更新)(哔哩哔哩);1、该软件可以轻松地帮助...
透视教程!aapoker怎么提... 透视教程!aapoker怎么提高中牌率(透视)透视脚本(切实有挂)1、玩家可以在aapoker怎么提...