.net – 在控制器中查看列表数据
发布时间:2020-05-24 20:58:57 所属栏目:asp.Net 来源:互联网
导读:我有一个看法,我在一个循环中呈现了部分视图.列表中有一个列表,部分视图与每个项目绑定.输入值后,我没有得到控制器列表的值. 这是我的看法 table id=resourceRequirement class=table width=100% border=0 thead tr style=background-color:#dfdfdf;
|
我有一个看法,我在一个循环中呈现了部分视图.列表中有一个列表,部分视图与每个项目绑定.输入值后,我没有得到控制器列表的值. 这是我的看法 <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; }
}
请告知我什么时候试图从列表中获取列表,我将该列表作为null. 解决方法您使用foreach循环,部分生成不带索引器的重复名称属性(因此无法绑定到集合)和重复的id属性(无效的html).而不是部分视图,请使用EditorTemplate.将您当前的部分视图重命名为ResourceRequirement.cshtml(即匹配类的名称),并将其放在/ Views / Shared / EditorTemplates文件夹(或/ Views / yourController / EditorTemplates文件夹中) 然后在主视图中,删除foreach循环并替换它 <tbody>
@Html.EditorFor(m => m.ResourceRequirement)
</tbody>
EditorFor()方法接受IEnumerable< T>并为您的集合中的每个项目生成正确的html.如果您检查html,您将在窗体控件中看到正确的名称属性 <input type="text" name="ResourceRequirement[0].priority" .... /> <input type="text" name="ResourceRequirement[1].priority" .... /> <input type="text" name="ResourceRequirement[2].priority" .... /> 等等,当您提交表单时,它将绑定到您的模型(将其与您当前生成的内容进行比较) (编辑:安卓应用网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- asp.net-mvc – 可以将页脚添加到MVCContrib网格吗?
- asp.net – Page_Load中的Response.Redirect
- asp.net-mvc – 如何在扩展方法中使用HTML帮助器方法?
- asp.net – #Eval if语句在中继器
- asp.net-mvc-3 – 可用会员入门套件/成员资料用于ASP.NET M
- asp.net – 防止XSS(跨站脚本)
- ASP.net MVC v2 – 调试模型绑定问题 – BUG?
- asp.net-core – 如何在ASP.NET 5中使用“旧”依赖项
- 如何从ASP.NET MVC视图显示存储在数据库中的HTML?
- asp.net – MVC 6 – RC1到RC2的更改
推荐文章
站长推荐
- 实体框架 – MVC3应用程序/服务层/存储库层/ POC
- asp.net – 抽象通用ODataController类导致“没有
- asp.net-mvc – 从业务逻辑类重定向asp.net mvc页
- asp.net-core – .NET Core 1.0 – 如何使用xUni
- asp.net-mvc – 如何添加日志到MVC4 WebApi
- asp.net-mvc – asp.net mvc 4从控制器按钮调用方
- asp.net-mvc – 防止Google分析在开发环境ASP.NE
- asp.net-mvc-3 – 如何增加会话超时MVC 3
- asp.net – 在MVC5应用程序中使用OWIN软件包的好
- asp.net-mvc – 在MVC3中使用Html.BeginForm是什
热点阅读
