asp.net mvc - MVC 4 Views, common or specific? -


this first time using mvc, first time writing web application well.

so far, managed have view list of employees, , edit view employee model.

if had 25 models need displayed lists, , edited, have create 50 different views?

or there way have 1 common list view , 1 common edit view?

(edit below)

solved list view issue. sorry long code.

i created modelpropertyinfo class describes model properties. added label, might add more properties "format", "inputtype", ...

// model field information class. used views display model info public class modelpropertyinfo {     public modelpropertyinfo() { }      public string name { get; set; }     public string label { get; set; }  } 

then showinlistattribute attribute class decorate model properties want appear in list view

// attribute class used specify labels model fields public class showinlistattribute : attribute {     public showinlistattribute(string header)     {         header = header;     }      public string header { get; set; } } 

and modelbase class models inherit. class give ability property value class passing name string

// base class models public class modelbase {     public static list<modelpropertyinfo> modelproperties(type modeltype)     {         list<modelpropertyinfo> result = new list<modelpropertyinfo>();         foreach (propertyinfo pi in modeltype.getproperties())         {             showinlistattribute att = (showinlistattribute)pi.getcustomattributes(typeof(showinlistattribute), true).firstordefault();             if (att != null)                 result.add(new modelpropertyinfo { label = att.header, name = pi.name });         }         return result;     }      public object getpropertyvalue(string propname)     {         return this.gettype().getproperty(propname).getvalue(this, null);     } } 

now, here's employee model class

[table("employee")] public class employee : modelbase {     [key]     [databasegeneratedattribute(databasegeneratedoption.identity)]     public decimal id { get; set; }      [showinlist("first name")]     public string firstname { get; set; }      [showinlist("last name")]     public string lastname { get; set; }      public decimal departmentid { get; set; }      [showinlist("department")]     [databasegeneratedattribute(system.componentmodel.dataannotations.schema.databasegeneratedoption.computed)]     public string departmentname { get; set; } } 

so, put above use, here's index method in employeecontroller

public actionresult index() {     viewbag.columns = modelbase.modelproperties(typeof(employee));     viewbag.title = "employee list";     return view("listshared", db.employees.tolist()); } 

finally, result, sharedlistview use display list of model want

@using sharedlistview.models   @model ienumerable<modelbase>    <h2>@viewbag.title</h2>  <p>     @html.actionlink("create new", "create") </p> <table>     <tr>         @foreach (modelpropertyinfo col in viewbag.columns)         {             <th>                 @col.label             </th>         }         <th></th>     </tr>      @foreach (var item in model) {         <tr>             @foreach (modelpropertyinfo col in viewbag.columns)             {                 <td width='100px'>                     @item.getpropertyvalue(col.name).tostring()                 </td>             }             <td>                 @html.actionlink("edit", "edit", new { id=item.getpropertyvalue("id") }) |                 @html.actionlink("details", "details", new { id=item.getpropertyvalue("id") }) |                 @html.actionlink("delete", "delete", new { id=item.getpropertyvalue("id") })             </td>         </tr>     } </table> 

still stuck on common edit view, appreciated.

again, sorry long edit.

you don't need that. asp.net mvc supports contentfor method , editorfor method. in case, need designing view models, , afterwards on view, can use like

@html.contentfor(model.employee) // display - mean, should read-only @html.editorfor(model.employee) // editing. 

you can see post topic here


Comments

Popular posts from this blog

python - How to create a legend for 3D bar in matplotlib? -

java - Multi-Label Document Classification -

php - Dynamic url re-writing using htaccess -