如何从视图中获取表数据是asp.net mvc 4(How to get table data from view is asp.net mvc 4)

编程入门 行业动态 更新时间:2024-10-27 20:27:56
如何从视图中获取表数据是asp.net mvc 4(How to get table data from view is asp.net mvc 4)

我的问题是如何从视图中将表数据恢复到控制器?

我在我的模型中上课:

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

更多推荐

本文发布于:2023-08-07 15:57:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1464918.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:视图   数据   asp   net   view

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!