asp.net mvc 2 - C# MVC 2 HtmlHelper to generate Table Columns with Model Binding -


i'm after mvc2 custom htmlhelper allow me dynamically create x amount of columns given data.

say instance have zoo class contains list of animals (which contains sub class). ex:

public class zoo {   public list<animals> myanimals; }  public class animals  {   public string year;    public warmclimate warm;    public class warmclimate {     public string hippo;     public string zebra;     public string elephant;     etc...   } } 

and want create table similar following. creating column every list of animals have. 4 columns worth of data or 30.

   |             | 2011 | 2012 | 2013 | 2014 |    | hypo        | 6    | 1    | 7    |  0   |    | zebra       | 1    | 1    | 2    |  1   |    | elephant    | 1    | 1    | 3    |  0   | 

i have this; quite basic. loop iterates on list grabbing defined property value. if there 100 animals in class, page 100 loops nightmare.

<tr>     <td>zebras: </td>     <%          (int = 0; < model.myanimals.count; i++)         {     %>         <td><%= html.editorfor(x => x.myanimals[i].warm.zebra) %></td>     <%         }     %> </tr> 

i replace custom htmlhelper allow me like:

<tr>   <td>zebras: </td>   <%= html.mycusthelper( property?? , list??, expression??) %> </tr> 

mycusthelper return appropriate html name attribute populated correctly model binding, when use html.editorfor() helper, ex:

<td><input name="myanimals[2].warm.zebra" id="myanimals_2__warm_zebra" type="text" value="1"></td> etc.... etc... 

maybe i'm going wrong, assume there simple way output repeatable columns worth of data.

can out efficient way of doing this?

i don't see why need custom helper when editortemplate should want. still use editorfor, use template it.

basically, specify template name editorfor:

<tr>     <td>zebras: </td>     <%= html.editorfor(x => x.myanimals, "zebras") %> </tr> 

i have long forgotten webforms syntax this, this. create editortemplates folder , in create file called zebras.ascx , add appropriate webforms @directives typed model.

<td><%= html.editorfor(x => x.warm.zebra) %></td> 

editor templates automatically iterate on collection, don't need index, , automatically generates correct naming convention.

read 5 part series here:

http://bradwilson.typepad.com/blog/2009/10/aspnet-mvc-2-templates-part-1-introduction.html

unfortunately, stopped being able think in webforms code quite time ago, can't give real example.


Comments

Popular posts from this blog

javascript - DIV "hiding" when changing dropdown value -

Does Firefox offer AppleScript support to get URL of windows? -

android - How to install packaged app on Firefox for mobile? -