asp.net mvc - How to show complete table using model in MVC? -
my database table has 50 rows, how show tables on view using model? when tried show product data on view. shows last row of table, how can show rows in view?
//model : product properties public class allitems { public string id { get; set; } public string name { get; set; } public string qty { get; set; } public string price { get; set; } public string imgname { get; set; } public string prddesc { get; set; } } //controller : product data action manager public actionresult viewallitems() { string[] name = new string[10]; datatable dtproducts = data.getallitems(); (int = 0; < dtproducts.rows.count; i++) { allitems allitems = new allitems { id = dtproducts.rows[i][0].tostring(), name = dtproducts.rows[i][1].tostring(), qty = dtproducts.rows[i][2].tostring(), price = dtproducts.rows[i][3].tostring(), prddesc = dtproducts.rows[i][4].tostring(), }; viewdata["allitems"] = allitems; } return view(); } //view show product data @{ allitems allitems = (allitems)viewdata["allitems"]; (int = 0; < 5; i++) { <table style="border: 1px solid lightgrey; border-collapse: collapse"> <tr> <td> <h3>@allitems.id</h3> </td> <td> <h3>@allitems.name</h3> </td> <td> <h3>@allitems.price</h3> </td> <td> <h3>@allitems.qty</h3> </td> <td> <h3>@allitems.imgname</h3> </td> <td> <h3>@allitems.prddesc</h3> </td> </tr> <br /> </table> } }
is there way can make array of model class , show properties?
try this. modified code use model instead of viewdata.
public class allitems { public string id { get; set; } public string name { get; set; } public string qty { get; set; } public string price { get; set; } public string imgname { get; set; } public string prddesc { get; set; } } //controller : product data action manager public actionresult viewallitems() { string[] name = new string[10]; datatable dtproducts = data.getallitems(); list<allitems> items = new list<allitems>(); (int = 0; < dtproducts.rows.count; i++) { allitems allitems = new allitems { id = dtproducts.rows[i][0].tostring(), name = dtproducts.rows[i][1].tostring(), qty = dtproducts.rows[i][2].tostring(), price = dtproducts.rows[i][3].tostring(), prddesc = dtproducts.rows[i][4].tostring(), }; items.add(allitems); } return view(items); } //view show product data @model list<allitems> @foreach (var item in model) { <table style="border: 1px solid lightgrey; border-collapse: collapse"> <tr> <td> <h3>@item.id</h3> </td> <td> <h3>@item.name</h3> </td> <td> <h3>@item.price</h3> </td> <td> <h3>@item.qty</h3> </td> <td> <h3>@item.imgname</h3> </td> <td> <h3>@item.prddesc</h3> </td> </tr> <br /> </table> }
Comments
Post a Comment