是否有可能有一個編輯多個記錄的視圖,就像index.cshtml視圖通過記錄循環顯示它們一樣(如下所示)?asp.net mvc更新多條記錄
@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);
}
我堅持控制器,如果有人可以提供任何幫助?
再次感謝,
馬克
月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);
}
嗨 - 我與控制器掙扎 - 你將能夠幫助嗎?我的模型是:public class objectives { public int ID {get;組; } public int tvid {get;組; } public string tlnt {get;組; } public DateTime month {get;組; } public string objective {get;組; } public int score {get;組; } public int possscore {get;組; } public string comments {get;組; }} 我 – Mark
控制器,其中我卡是:[HttpPost] 公共的ActionResult編輯(目標的目標) { 如果(ModelState.IsValid) { db.Entry(目標)= .STATE EntityState.Modified; foreach(objective objective in objectives.objective) {} db.SaveChanges(); return RedirectToAction(「Index」); } return View(objectives); } – Mark
這裏有多個項目在這裏? – Shyju