[英]Something Like Win App DataGridView in ASP.NET MVC3 With Update All Elements
我在ASP.NET MVC3和每行前面的索引视图中都有一个简单的模型,出现3个链接(“编辑”,“详细信息”,“删除”)。使用“编辑链接”,页面导航到编辑视图,您可以编辑该特定行,所以我很有趣在一起编辑所有行,就像通过DataGridView在Win应用程序中一样,例如在“索引”视图中的表是“可编辑的”和“全部更新”按钮,保存所有行的编辑,是否有人对此有任何想法?
you can edit index view, like the following: 您可以编辑索引视图,如下所示:
1- Replace @Html.DisplayFor
with @Html.EditorFor
for that columns you want to edit, 1-替换
@Html.DisplayFor
与@Html.EditorFor
您要编辑的列,
2- Add @using (Html.BeginForm()){}
to included all of <table>
tag, 2-添加
@using (Html.BeginForm()){}
以包含所有<table>
标记,
3- Add a submit like this: <input type="submit" value="Updata All" />
inside of BeginForm()
block 3-像这样添加一个提交:
BeginForm()
块内的<input type="submit" value="Updata All" />
BeginForm()
<input type="submit" value="Updata All" />
4- Add a new [HttpPost]
for Index action in your controller and handle edits you can use something like this: 4-在您的控制器中为索引操作添加一个新的
[HttpPost]
并处理编辑,您可以使用如下代码:
[HttpPost]
public ActionResult Index(FormCollection collection)
{
string[] Descriptions = collection.GetValues("item.Description");
for (int i = 1; i <= Descriptions.Length; i++)
{
MyModel element = db.MyModels.Find(i);
element.Description = Descriptions[i - 1];
db.Entry(element).State = EntityState.Modified;
db.SaveChanges();
}
return View(db.MyModels.ToList());
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.