我有一个基于 Nominees 的 Viewmodel。而且我可以为 View 模型指定多个提名人。
我想从 View 中填充 Ilist。这是我的 View 模型
public class DebitViewModel:IValidatableObject
{
public string AgentName { get; set; }
public Debit Debit { get; set; }
public Policy Policy { get; set; }
public PolicyType PolicyType { get; set; }
public Customer Customer { get; set; }
public IList<PolicyType> PolicyTypes { get; set; }
public List<Nominee> Nominees { get; set; }
public Dictionary<int,string> OccupationTypes { get; set; }
}
当我按下 submit 时,我想自动填充所有 Nominess 。那么我应该如何通过 View 创建并使其自动填充列表?而不是单独的对象?
最佳答案
您可以使用编辑器模板:
@model DebitViewModel
@using (Html.BeginForm())
{
... some input fields for the other properties that we are not interested in
@Html.EditorFor(x => x.Nominees)
<button type="submit">OK</button>
}
然后为
Nominee
定义自定义编辑器模板模型 ( ~/Views/Shared/EditorTemplates/Nominee.cshtml
) 将自动为 Nominees
的每个元素呈现收藏:@model Nominee
<div>
@Html.EditorFor(x => x.FirstName)
@Html.EditorFor(x => x.LastName)
...
</div>
关于asp.net-mvc - 从 Mvc 3 View 填充 List<Objects>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9800211/