获取视图控制器列表数据 [英] Get list data on view in controller
问题描述
我有我已经在一个循环中渲染的局部视图的视图。有一个列表,局部视图都被绑定在一个列表中的每个项目。输入值后,我没有控制器获取列表的价值。
I have a view in which I have rendered partial view in a loop. There is a list and partial view have is binded with each item in a list. I am not getting the value of the list on controller after value is entered.
下面是我的看法:
<table id="resourceRequirement" class="table" width="100%" border="0">
<thead>
<tr style="background-color:#dfdfdf;">
<td><div align="center">PRIORITY</div></td>
<td><div align="center">SYSTEM RESOURCE / COMPONENT</div></td>
<td><div align="center">RECOVERY TIME OBJECTIVE</div></td>
</tr>
</thead>
<tbody>
@foreach (var item in Model.ResourceRequirement)
{
@Html.Partial("~/Views/Shared/_ResourceRequirement.cshtml", item)
}
</tbody>
</table>
下面是我的部分观点:
@model DisasterManagementSystem.Models.BusinessImpactAnalysis.ResourceRequirement
<tr>
<td>
@Html.TextBoxFor(m => m.priority)<br />
<div style="color:red;">
@Html.ValidationMessageFor(model => model.priority)
</div>
</td>
<td>
@Html.TextBoxFor(m => m.systemresource)<br />
<div style="color:red;">
@Html.ValidationMessageFor(model => model.systemresource)
</div>
</td>
<td>
@Html.TextBoxFor(m => m.receveryTime)<br />
<div style="color:red;">
@Html.ValidationMessageFor(model => model.receveryTime)
</div>
</td>
</tr>
下面是我的列表:
public List<ResourceRequirement> ResourceRequirement { get; set; }
和类是在这里:
public class ResourceRequirement
{
[Required(ErrorMessage = "*")]
public string priority { get; set; }
[Required(ErrorMessage = "*")]
public string systemresource { get; set; }
[Required(ErrorMessage = "*")]
public string receveryTime { get; set; }
}
请指教,当我试图从模型上后我收到列表为空的列表中。
Please advise when i am trying to get the list from model on post I am getting the list as null.
推荐答案
您使用的foreach
循环,部分正在产生重复的名称
属性没有索引(所以不能绑定到一个集合)和复制 ID
属性(无效的HTML)。
You use of a foreach
loop and a partial is generating duplicate name
attributes without indexers (so cannot bind to a collection) and duplicate id
attributes (invalid html).
而不是一个局部视图,使用 EditorTemplate
。重命名当前的局部视图 ResourceRequirement.cshtml
(即以匹配类的名称),并将其放置在 /查看/共享/ EditorTemplates
文件夹(或 /查看/ yourController / EditorTemplates
文件夹)
Instead of a partial view, use an EditorTemplate
. Rename your current partial view to ResourceRequirement.cshtml
(i.e. to match the name of the class) and place it in the /Views/Shared/EditorTemplates
folder (or in the /Views/yourController/EditorTemplates
folder)
然后在主视图中,删除的foreach
循环,以
Then in the main view, remove the foreach
loop and replace it with
<tbody>
@Html.EditorFor(m => m.ResourceRequirement)
</tbody>
的 EditorFor()
方法接受的IEnumerable&LT; T&GT;
并生成正确的HTML为您收藏的每件。如果你检查HTML你将看到正确的名称与窗体控件的属性。
The EditorFor()
method accepts IEnumerable<T>
and generates the correct html for each item in your collection. If you inspect the html you will now see the correct name attributes in your form controls
<input type="text" name="ResourceRequirement[0].priority" .... />
<input type="text" name="ResourceRequirement[1].priority" .... />
<input type="text" name="ResourceRequirement[2].priority" .... />
等。其中,当你提交表单将绑定到你的模型(以比较这你的当前生成)
etc. which will bind to your model when you submit the form (compare this with what your currently generating)
这篇关于获取视图控制器列表数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!