c# - How to create html table from model class -
i new in mvc , learning mvc. not want use grid extension, rather want generate tabular ui html table. looking code , found hint.
this code not full code. here got.
<% if (model.count() > 0) { %> <table width="35%"> <thead><tr><th>species</th><th>length</th></tr></thead> <% foreach (var item in model) { %> <tr> <td><%= item.fishspecies%></td> <td align="center"><%=item.length%></td> </tr> <% } %> </table> <% } else { %> no fish collected. <%} %>
the problem not being able visualize how model class should , how populated controller. viewbag not used in code, rather generating table directly model class.
so can write small complete code me create html table , populate model directly without using viewbag?
need code model, controller , view too.
if want iterate through model , create table, first of change @model
of view this:
@model ienumerable<myprj.models.entityname>
then, should change action method supply model items view:
public actionresult index() { return view(db.entitynames.tolist()); }
and finally, iterate model , create table:
<table id="tblnews"> <tr> <th> @html.displaynamefor(model => model.property1) </th> // ... <th> @html.displaynamefor(model => model.propertyn) </th> </tr> @foreach (var item in model) { <tr> <td> @html.displayfor(modelitem => item.property1) </td> // ... <td> @html.displayfor(modelitem => item.propertyn) </td> </tr> } </table>
and model? model nothing class properties:
public class mymodel { [display(name = "property 1")] [required(errormessage = "property cannot empty")] public int property1 { get; set; } // ... [display(name = "property n")] [required(errormessage = "property cannot empty")] public string propertyn { get; set; } }
Comments
Post a Comment