asp.net mvc更新多条记录
是否有可能有一个编辑多个记录的视图,就像index.cshtml视图循环通过记录显示它们一样(如下所示)?
@foreach (var item in Model) { <tr> <td> @Html.DisplayFor(modelItem => item.tvid) </td> 因此,对于上面的每一行,它将与数据库中的不同行相关. 有谁知道有任何例子说明如何实现这一目标? 谢谢你的任何指示, 标记 UPDATE 模型: using System; using System.Collections.Generic; using System.Linq; using System.Web; namespace MvcObjectives.Models { public class objectives { public int ID { get; set; } public int tvid { get; set; } public string tlnt { get; set; } public DateTime month { get; set; } public string objective { get; set; } public int score { get; set; } public int possscore { get; set; } public string comments { get; set; } } } 控制器: [HttpPost] public ActionResult Edit(objectives objectives) { if (ModelState.IsValid) { db.Entry(objectives).State = EntityState.Modified; foreach (objective Objective in objectives.objective) { } db.SaveChanges(); return RedirectToAction("Index"); } return View(objectives); } 如果有人能提供任何帮助,我会坚持使用控制器吗? 再次感谢, 标记 第二次更新 将记录发送到视图的GET控制器是: // GET: /Objective/Edit/ public ActionResult Edit() { return View(db.objectives.ToList()); } POST控制器(从视图中回发值的位置)是: // POST: /Objective/Edit/ [HttpPost] public ActionResult Edit(List<objectives> objectives) { if (ModelState.IsValid) { // the next part is where I am stuck - how to loop through the returned objectives,and update the records in the database db.Entry(objectives).State = EntityState.Modified; foreach (objectives obj in objectives) { var tempObj = (from objv in db.objectives where objv.ID==obj.ID select objv).First(); } // to do - how to save the updates sent back???? return RedirectToAction("Index"); } return View(objectives); } 解决方法
使用编辑器模板
假设您的ViewModel / Model看起来像这样 public class UserViewModel { public int UserId { set;get;} public string Name { set;get;} public IEnumerable<Address> Addresses { set;get;} public UserViewModel() { if(this.Addresses==null) this.Addresses=new List<Address>(); } } public class Address { public int AddressID { set;get;} public string AddressLine1 { set;get;} } 现在创建一个名为addresses.cshtml的编辑器模板,其中包含以下内容. @model YourNameSpace.Address @Html.TextBoxFor(x => x.AddressLine1) 在主视图中,您可以将其称为 @model UserViewModel @using (Html.BeginForm()) { //other elements @Html.EditorFor(m=>m.Addresses) <input type="submit" value="Save" /> } 现在,您将获得HttpPost Ation方法中的数据 [HttpPost] public ActionResult Save(UserViewModel model) { foreach (Address address in model.Addresses) { //now check for address.AddressLine here } } 编辑:根据用户对问题的评论和更新. (请求OP:下次发布问题时,请在第一时间将所有相关详细信息包含在问题中.) 创建一个ViewModel来包装Objective类的List. public class ObjectivesEdit { public IEnumerable<Objective> Objectives { set; get; } public ObjectivesEdit() { if (Objectives == null) Objectives = new List<Objective>(); } } 在您的GET Action方法中,将此Wrapper View模型发送到视图,并填充值 public ActionResult Edit() { ObjectivesEdit objEdit = new ObjectivesEdit(); List<Objective> objList = new List<Objective>(); // you can replace this manual filling with data from database objList.Add(new Objective { ID = 1,score = 65 }); objList.Add(new Objective { ID = 2,score = 43 }); objList.Add(new Objective { ID = 3,score = 78 }); objEdit.Objectives = objList; return View(objEdit); } 您的编辑器模板应如下所示.它应该命名为objective.cshtml @model EditorTemplateDemo.Models.Objective <p> Score for @Model.ID is @Html.TextBoxFor(x => x.score) @Html.HiddenFor(x => x.ID) </p> 而你的主要观点 @model EditorTemplateDemo.Models.ObjectivesEdit @using (Html.BeginForm()) { @Html.EditorFor(x=>x.Objectives) <input type="submit" value="Save" /> } 最后,您的HTTPPOST操作方法将如下所示 [HttpPost] public ActionResult Edit(ObjectivesEdit model) { if (model.Objectives != null) { // Put a break point here and you will see the posted data foreach (var item in model.Objectives) { context.Entry(item).State = EntityState.Modified; } //Save and redirect context.SaveChanges(); return RedirectToAction("Index"); } return View(model); } 这应该工作.测试. 为了避免进一步的问题,我正在分享上述代码here的工作示例.请在代码中使用visual studio断点来查看正在发布的值. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
- asp.net – 我可以在卫星装配中组合本地资源吗?
- asp.net-mvc – Paypal REST API – 在付款过程中传递参数
- asp.net-identity – ASP.net Identity 2.1获取所有角色的用
- 检查是否在IIS中托管Asp.Net(Core)应用程序
- ASP.NET MVC 4自定义HTML Helpers文件夹位置
- asp.net web.config加密解密方法
- ASP.NET FileUpload Control – 缓冲到磁盘时的安全问题
- asp.net-mvc – 用户个人头像asp.net mvc identity
- efcore 新特性 SaveChanges Events
- .net – 将数据写入App_Data