3

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 ?

2 Answers 2

1

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>
Sign up to request clarification or add additional context in comments.

4 Comments

it didnt work since its a list . a List of Nominees can't be shown by Nominee editor template :( i tried it its not showing the template fields .Where as if i do the same for single Nominee. It shows all the template fields :)
@Joy, you probably did a mistake with the naming of your custom editor template. The name and location is very important. It must be ~/Views/Shared/EditorTemplates/Nominee.cshtml and automatically the template will be rendered for each element of the collection.
But i saw only single nominee was working fine. i mean to say @Html.EditorFor(x => x.Nominee) is working fine . if my editor template name and location would have been wrong then even it wont render single Nominee as wel :) but its working all right . The only problem is with public List<Nominee> Nominees :)
@Joy, you should use @Html.EditorFor(x => x.Nominees) and not @Html.EditorFor(x => x.Nominee). And if your template name and location is wrong it will render a nominee. It's just that it will use the default template, not your custom template.
0

say for example the Nominee looks like

public class Nominee{
 public int Id{get;set;}
 public string Name{get;set;}
 public int Age {get;set;}
}

the view would look like

@for (int i = 0; i < Model.Nominees.Count(); i++)
{ 
<tr>                                                           
  <td>@Html.TextBoxFor(m => m.Nominees[i].Name)</td>
  <td>@Html.TextBoxFor(m => m.Nominees[i].Age)</td>
</tr>
}

read more about model binding to a list

1 Comment

Yes i know but i need to populate the list on HttpPost event . Would it populate the Nominess list automatically ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.