填充列表<对象和GT;从MVC 3视图 [英] Populate List<Objects> From Mvc 3 view

查看:112
本文介绍了填充列表<对象和GT;从MVC 3视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个基于代理人一个ViewModel。而且我可以有一个视图模型多名获提名人。

I Have a Viewmodel based on Nominees . And i can have Multiple Nominees for the viewmodel.

我要填充的IList从视图。这里是我的ViewModels

I want to populate the Ilist From the view . Here are my viewmodels

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

我要自动填充所有Nominess当我preSS提交。所以我应该如何通过视图中创建并使其自动自动填充列表?而不是serparate对象?

I want to populate all Nominess automatically when i press submit . so how should i create by view and make it automatically populate List automatically ? instead of serparate objects ?

推荐答案

您可以使用编辑器模板:

You could use editor templates:

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

和则定义了提名的自定义编辑模板模式(〜/查看/共享/ EditorTemplates / Nominee.cshtml )将自动被渲染为(代理人)集合中的每个元素:

and then you define a custom editor template for the Nominee model (~/Views/Shared/EditorTemplates/Nominee.cshtml) which will automatically be rendered for each element of the Nominees collection:

@model Nominee

<div>
    @Html.EditorFor(x => x.FirstName)
    @Html.EditorFor(x => x.LastName)
    ...
</div>

这篇关于填充列表&LT;对象和GT;从MVC 3视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆