是否可以有一个用于编辑多个记录的视图,就像 index.cshtml 视图循环遍历记录以显示它们一样(如下所示)?
@foreach (var item in Model) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.tvid)
</td>
因此,对于上面的每一行,它将与数据库中的不同行相关。
有谁知道任何例子表明如何实现这一目标?
感谢您的任何指点,
标记
更新
模型:
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);
}