asp.net-mvc – 如何使基于列表的编辑器模板正确绑定POST操作?
发布时间:2020-05-25 08:18:32 所属栏目:asp.Net 来源:互联网
导读:我有一个模型ApplicantBranchList,它在较大的模型中用作属性,如下所示: [Display(Name = Where would you want to work?)]public ApplicantBranchList PreferedBranches { get; set; } ApplicantBranchList: public class ApplicantBran
|
我有一个模型ApplicantBranchList,它在较大的模型中用作属性,如下所示: [Display(Name = "Where would you want to work?")]
public ApplicantBranchList PreferedBranches { get; set; }
ApplicantBranchList: public class ApplicantBranchList : ViewModel
{
public ApplicantBranchItem HeaderItem { get; set; }
public ApplicantBranchList()
{
HeaderItem = new ApplicantBranchItem();
}
public void MapFromEntityList(IEnumerable<ApplicantBranch> applicantBranches)
{
var service = new BranchService(DbContext);
var selectedIds = applicantBranches.Select(b => b.BranchId);
Items = service.ReadBranches()
.Where(i => !i.IsDeleted)
.Select(p => new ApplicantBranchItem { BranchName = p.Name,WillWorkAt = selectedIds.Contains(p.Id) });
}
public IEnumerable<ApplicantBranchItem> Items { get; set; }
}
ApplicantBranchList有自己的编辑器模板,以及ApplicantBranchList中每个项目的内部编辑器模板: 查看/共享/ EditorTemplates / ApplicantBranchList.cshtml: @model Comair.RI.UI.Models.ApplicantBranchList
<table>
<tr>
<th style="display: none;"></th>
<th>
@Html.DisplayNameFor(model => model.HeaderItem.BranchName)
</th>
<th>
@Html.DisplayNameFor(model => model.HeaderItem.WillWorkAt)
</th>
</tr>
@foreach (var item in Model.Items)
{
@Html.EditorFor(m => item)
}
</table>
查看/共享/ EditorTemplates / ApplicantBranchItem.cshtml: @model Comair.RI.UI.Models.ApplicantBranchItem
<tr>
<td style="display: none;">
@Html.HiddenFor(m => m.BranchId)
</td>
<td>
@Html.DisplayFor(m => m.BranchName)
</td>
<td>
@Html.EditorFor(m => m.WillWorkAt)
</td>
</tr>
此编辑器在视图中正确呈现,但在后期操作中: public ActionResult Create(ApplicantProfileModel model)
{
if (ModelState.IsValid)
{
var branches = model.PreferedBranches;
PreferedBranches.Items为null. 我究竟做错了什么? 解决方法问题是ASP.NET无法弄清楚如何绑定到Model.Items属性.要修复它替换: public IEnumerable<ApplicantBranchItem> Items { get; set; }
有了这个: public List<ApplicantBranchItem> Items { get; set; }
而不是: @foreach (var item in Model.Items)
{
@Html.EditorFor(m => item)
}
使用这一个: @for (var i = 0; i < Model.Items.Count; i++)
{
@Html.EditorFor(model => model.Items[i]) // binding works only with items which are accessed by indexer
} (编辑:安卓应用网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- .net – 如何获得程序集最后修改日期?
- .net – 什么可以解释托管堆上超过5,000,000个System.WeakR
- asp.net-mvc-3 – 从ViewBag添加@ Html.TextBox值
- ASP.NET Kendo UI上传
- 当调用ASP.NET System.Web.HttpResponse.End()时,当前线程中
- ASP.NET Core的Keycloak客户端
- ASP.NET – UpdatePanel和JavaScript
- asp.net-mvc – 在布局视图中获取当前的ApplicationUser
- asp.net – gzip压缩在Windows Azure网站
- asp.net – 浏览器关闭后的身份验证/会话cookie删除
推荐文章
站长推荐
- asp.net – IIS表达请求的执行时间要长4倍
- asp.net-mvc – 什么时候使用ViewData而不是View
- 用AJAX实现的无刷新的分页实现代码(asp.net)
- 如何在asp.net webforms捆绑中将cdN添加到bundle
- asp.net-mvc-4 – Cshtml无法解析引用
- asp.net-mvc-3 – ASP.NET MVC 3本地化验证消息在
- asp.net – Oracle.ManagedDataAccess:TNS:无法
- 如何在Asp.Net Mvc中进行Basecamp风格的账户?
- linq – ASP.NET Web API GET方法:为单个参数传
- asp.net-mvc – ModelState.AddModelError编码HT
热点阅读
