asp.net mvc - View strongly-typed with ViewModel doesn't generate fields automatically -


when create view , bind directly 1 class has properties want show on view, fields (textboxes, etc.) created automatically. when create viewmodel encapsulate more 1 object data, doesn't happen. there way working specific object inside viewmodel?

thanks.

thanks commenters teasing out answer details.

mvc's default editorfor "master" template, object.ascx, has if statement in place prevent happening.

to change behavior need replace base /editortemplates/object.ascx template own. replica of template baked mvc:

<%@ control language="c#" inherits="system.web.mvc.viewusercontrol" %> <% if (model == null) { %>     <%= viewdata.modelmetadata.nulldisplaytext %> <% } else if (viewdata.templateinfo.templatedepth > 1) { %>     <%= viewdata.modelmetadata.simpledisplaytext %> <% } else { %>     <table cellpadding="0" cellspacing="0" border="0">     <% foreach (var prop in viewdata.modelmetadata.properties.where(pm => pm.showfordisplay && !viewdata.templateinfo.visited(pm))) { %>         <% if (prop.hidesurroundinghtml) { %>             <%= html.display(prop.propertyname) %>         <% } else { %>             <tr>                 <td>                     <div class="display-label" style="text-align: right;">                         <%= prop.getdisplayname() %>                     </div>                 </td>                 <td>                     <div class="display-field">                         <%= html.display(prop.propertyname) %>                     </div>                 </td>             </tr>           <% } %>     <% }   %>     </table> <% } %> 

this line:

<% } else if (viewdata.templateinfo.templatedepth > 1) { %> 

tell template go down single level of object graph. replace 1 2 or remove entirely change how far mvc drill down.

more details template can found here: http://bradwilson.typepad.com/blog/2009/10/aspnet-mvc-2-templates-part-4-custom-object-templates.html

( man, should create macro linking brad wilson's stuff, time ) ;)


Comments

Popular posts from this blog

c++ - How do I get a multi line tooltip in MFC -

asp.net - In javascript how to find the height and width -

c# - DataTable to EnumerableRowCollection -