asp.net mvc - Mvc 3: displaying viewmodel items in a view -
i have class this
public class ameinvoice { public datetime invoicedate { get; set; } public string invoicenumber { get; set; } public string accountnumber { get; set; } public double amount { get; set; } public double amountdue { get; set; } }
i have viewmodel this
public class ameinvoiceviewmodel { public list<ameinvoice> ppinvoices { get; set; } public double otheramount { get; set; } public double totaldue { get; set; } }
my action result method in controller
public actionresult index () { .... .... .... list<ameinvoice> prideinvoices = new list<ameinvoice>(); while (reader.read()) { prideinvoices.add(new ameinvoice() { invoicedate = convert.todatetime(reader["invoicedate"]), invoicenumber = reader["invoicenumber"].tostring(), accountnumber = reader["account"].tostring(), amount = convert.todouble(reader["amount"]), amountdue = convert.todouble(reader["amountdue"]) }); } var myviewmodel = new ameinvoiceviewmodel(); myviewmodel.ppinvoices = prideinvoices; myviewmodel.otheramount = 20.20; myviewmodel.totaldue = 30.20; return view(myviewmodel); }
in view have this
@model list<inscmm.web.model.ameinvoiceviewmodel> @{ viewbag.title = "index"; } <h2>index</h2> <table> @foreach (inscmm.web.model.ameinvoiceviewmodel objuser in model) { <tr> <td>@objuser.</td> <td>@objuser.</td> <td>@objuser.</td> <td>@objuser.</td> <td>@objuser.</td> </tr> } </table>
my questions: new mvc , want able display invoicedate, invoicenumber, accountnumber, amount, amount due in table in view. @ moment if objuser. (i don't these fields) please assist. right path?
your view must typed view model passing, not list:
@model inscmm.web.model.ameinvoiceviewmodel
and then:
<table> @foreach (var objuser in model.ppinvoices) { <tr> <td>@objuser.invoicedate</td> <td>@objuser.invoicenumber</td> <td>@objuser.accountnumber</td> <td>@objuser.amount</td> <td>@objuser.amountdue</td> </tr> } </table> <div> total due: @html.displayfor(x => x.totaldue) </div>
Comments
Post a Comment