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 ?
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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With