Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Populate List<Objects> From Mvc 3 view

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

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

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 ?

like image 815
Joy Avatar asked Apr 24 '26 00:04

Joy


1 Answers

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

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>
like image 97
Darin Dimitrov Avatar answered Apr 26 '26 07:04

Darin Dimitrov