从 Mvc 3 视图填充列表

2023-12-20

我有一个基于 Nominees 的 Viewmodel 。我可以为视图模型指定多个提名者。

我想从视图中填充 Ilist 。这是我的视图模型

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; }        
}

我想在按下“提交”时自动填充所有提名者。那么我应该如何通过视图创建并使其自动填充列表?而不是单独的对象?


您可以使用编辑器模板:

@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 model (~/Views/Shared/EditorTemplates/Nominee.cshtml),它将自动为每个元素呈现Nominees收藏:

@model Nominee

<div>
    @Html.EditorFor(x => x.FirstName)
    @Html.EditorFor(x => x.LastName)
    ...
</div>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

从 Mvc 3 视图填充列表 的相关文章

随机推荐