简体   繁体   中英

asp.net MVC “Add View” wizard won't pre-populate fields when using a ViewModel

In VS 2010, When you use the "Add View" wizard to create an Edit view with a strongly typed view, such as Models.Person, the template generates all of Person fields for you.

If you use a view model instead, like this:

public class PersonVM
{
   public Person person;
   public List<Team> TeamList = new TeamServices().TeamPickList();
   ...
}

the template wont create all of the fields for Model.person.

Is there a way to make that work?

Not automatically.

Easiest method is to create a new View, select Team as the view data class, Select 'List' as the view content. Then you could cut and paste the markup generated from this view into the one you have already created.

If you use the List template it will normally create a table and iterate over an IEnumerable Model. You can also use one of the helpers and/or custom templates in your CodeTemplates folder:

<% Html.DisplayForModel(); %>

If you need to edit:

<% Html.EditorForModel(); %>

If you're having trouble with the list, maybe start with one of the helpers?

<%: Html.DropDownListFor(model => model.TeamList, new SelectList(Model.TeamList)) %>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM