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);
}
誰かが何か援助を提供できるなら、私はコントローラーに固執していますか?
再度、感謝します、
マーク
2回目の更新
レコードをビューに送信する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);
}