我的问题是如何从视图中将表数据恢复到控制器?
我在我的模型中上课:
public class Company { public string Name { get; set; } public int ID { get; set; } public string Address { get; set; } public string Town { get; set; } }我将公司名单传递给我的观点:
@model IEnumerable<MyTestApp.Web.Models.Company> .... @using (Html.BeginForm("Edit", "Shop")) { <table id="example"> <thead> <tr> <th> @Html.DisplayNameFor(model => model.Name) </th> <th> @Html.DisplayNameFor(model => model.Address) </th> <th> @Html.DisplayNameFor(model => model.Town) </th> </tr> </thead> <tbody> @foreach (var item in Model) { <tr> <td> @Html.EditorFor(modelItem => item.Name) </td> <td> @Html.EditorFor(modelItem => item.Address) </td> <td> @Html.EditorFor(modelItem => item.Town) </td> </tr> } </tbody> </table> <input type="submit" value="Submit" /> }一切看起来都不错,但我无法理解如何在控制器中获取修改后的数据? 我用这些方法:
public ActionResult Edit(IEnumerable<Company> companies) { // but companies is null // and ViewData.Model also is null return RedirectToAction("SampleList"); }我需要访问修改过的对象,我做错了什么?
更新:感谢webdeveloper ,我只需要使用'for'循环而不是'foreach'循环。 正确的版本是
<tbody> @for (int i = 0; i < Model.Count(); i++ ) { <tr> <td> @Html.EditorFor(modelItem => modelItem[i].Name) </td> <td> @Html.EditorFor(modelItem => modelItem[i].Address) </td> <td> @Html.EditorFor(modelItem => modelItem[i].Town) </td> </tr> } </tbody>My question is how to get table data back to the controller from view?
I have class in my model:
public class Company { public string Name { get; set; } public int ID { get; set; } public string Address { get; set; } public string Town { get; set; } }and I pass list of Companies to my view as:
@model IEnumerable<MyTestApp.Web.Models.Company> .... @using (Html.BeginForm("Edit", "Shop")) { <table id="example"> <thead> <tr> <th> @Html.DisplayNameFor(model => model.Name) </th> <th> @Html.DisplayNameFor(model => model.Address) </th> <th> @Html.DisplayNameFor(model => model.Town) </th> </tr> </thead> <tbody> @foreach (var item in Model) { <tr> <td> @Html.EditorFor(modelItem => item.Name) </td> <td> @Html.EditorFor(modelItem => item.Address) </td> <td> @Html.EditorFor(modelItem => item.Town) </td> </tr> } </tbody> </table> <input type="submit" value="Submit" /> }And everything looks ok, but I can't understand how to get modified data in the controller? I used these approaches:
public ActionResult Edit(IEnumerable<Company> companies) { // but companies is null // and ViewData.Model also is null return RedirectToAction("SampleList"); }I need access to modified objects, what am I doing wrong?
UPDATE: Thanks to webdeveloper, I just needed use 'for' loop instead of 'foreach' loop. Right version is
<tbody> @for (int i = 0; i < Model.Count(); i++ ) { <tr> <td> @Html.EditorFor(modelItem => modelItem[i].Name) </td> <td> @Html.EditorFor(modelItem => modelItem[i].Address) </td> <td> @Html.EditorFor(modelItem => modelItem[i].Town) </td> </tr> } </tbody>最满意答案
请在这里查看我的答案: 在同一视图中更新多个项目或Darin Dimitrov回答。
您需要在呈现的html标记中具有index name属性的项目。 您还可以查看: 模型绑定到列表
Please, look at my answer here: Updating multiple items within same view OR for Darin Dimitrov answer.
You need items with index in name attribute in rendered html markup. Also you could look at: Model Binding To A List
更多推荐
发布评论