asp.net-mvc – MVC – 编辑对象列表
发布时间:2020-05-23 21:09:53 所属栏目:asp.Net 来源:互联网
导读:我在MVC中有以下类布局: public class ReportModel { ListSomeItem items; string value; string anotherValue;} 现在我在这种类型的MVC中创建一个强类型视图,并使可编辑的文本字段编辑每个值,并使用foreach循环填充文本字段以编辑someitem列表中的项目. 当
|
我在MVC中有以下类布局: public class ReportModel
{
List<SomeItem> items;
string value;
string anotherValue;
}
现在我在这种类型的MVC中创建一个强类型视图,并使可编辑的文本字段编辑每个值,并使用foreach循环填充文本字段以编辑someitem列表中的项目. 当我提交到httppost方法时,单个值在reportmodel对象中返回正常,但列表不会在对象中返回.该怎么做? 当我说httppost时,我指的是MVC发回的方法 [HttpPost]
public ActionResult EditReport(ReportModel report)
{
// Save the report in here after the update on the UI side
}
查看发布someitem列表的代码 if (Model.items != null && Model.items.Count > 0)
{
for (int i = 0; i < Model.items.Count; i++)
{
<div class="editrow">
<div class="edititem">
<div class="editor-label">
@Html.LabelFor(m => m.items.ElementAt(i).propertyOne)
</div>
<div class="editor-field">
@Html.TextBoxFor(m => m.items.ElementAt(i).propertyOne)
@Html.ValidationMessageFor(m => m.items.ElementAt(i).propertyOne)
</div>
</div>
<div class="edititem">
<div class="editor-label">
@Html.LabelFor(m => m.items.ElementAt(i).propertyTwo)
</div>
<div class="editor-field">
@Html.TextBoxFor(m => m.items.ElementAt(i).propertyTwo)
@Html.ValidationMessageFor(m => m.items.ElementAt(i).propertyTwo)
</div>
</div>
<div class="edititem">
<div class="editor-label">
@Html.LabelFor(m => m.items.ElementAt(i).propertyThree)
</div>
<div class="editor-field">
@Html.TextBoxFor(m => m.items.ElementAt(i).propertyThree)
@Html.ValidationMessageFor(m => m.items.ElementAt(i).propertyThree)
</div>
</div>
</div>
}
}
解决方法不要在lambda表达式中使用ElementAt(1)=>这会破坏您输入的字段名称.请阅读Kirill建议你的博客文章.所以你可以使用索引访问: for (int i = 0; i < Model.items.Count; i++)
{
<div class="editrow">
<div class="edititem">
<div class="editor-label">
@Html.LabelFor(m => m.items[i].propertyOne)
</div>
<div class="editor-field">
@Html.TextBoxFor(m => m.items[i].propertyOne)
@Html.ValidationMessageFor(m => m.items[i].propertyOne)
</div>
</div>
<div class="edititem">
<div class="editor-label">
@Html.LabelFor(m => m.items[i].propertyTwo)
</div>
<div class="editor-field">
@Html.TextBoxFor(m => m.items[i].propertyTwo)
@Html.ValidationMessageFor(m => m.items[i].propertyTwo)
</div>
</div>
<div class="edititem">
<div class="editor-label">
@Html.LabelFor(m => m.items[i].propertyThree)
</div>
<div class="editor-field">
@Html.TextBoxFor(m => m.items[i].propertyThree)
@Html.ValidationMessageFor(m => m.items[i].propertyThree)
</div>
</div>
</div>
}
当然,为了让索引器访问集合,假设您的items属性被声明为List< SomeItem>或SomeItem [].如果是IEnumerable< SomeItem>它不会起作用.因此,只需在视图模型上更改此属性的类型即可. (编辑:安卓应用网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- 如果ASP.NET应用程序的CustomErrors设置为Off,有没有办法以
- vbscript – 计算字符串中有多少个特定字符
- asp.net-mvc – 从基本控制器继承LINQ-to-SQL数据上下文
- asp.net-mvc – 在自定义编辑器中只包含一次脚本
- 有趣的项目理念为体面的开发想要了解更多Asp.Net
- asp.net – 什么导致页面呈现慢?
- asp.net-mvc – 在ASP.NET MVC ViewModel类中获取数据?
- iis-7.5 – IIS 7.5 Siteminder不保护ASP.net MVC请求
- 使用ASP.NET AJAX Control Toolkit设置焦点
- 从ASP.NET MVC应用程序中的Amazon SES发送电子邮件
推荐文章
站长推荐
- asp.net-mvc – ASP.NET身份与简单的会员优点和缺
- 在iis6.0中更改asp.net版本
- 点击图片,AJAX删除后台图片文件的实现代码(asp.n
- asp.net-membership – 使用SQL提供程序获取ASP.
- asp.net – 使CustomValidator与ValidationSumma
- 当ASP.NET验证失败时,更改文本框的css类
- 处理ASP.NET MVC中的异步请求
- asp.net-mvc – ASP.NET MVC – 为基本控制器中的
- asp.net-mvc – MSFT报告查看器(rdlc)是否可以使
- 实体框架 – 使用EF和WebAPI,如何返回一个ViewMo
热点阅读
