c# - List and create in same view in asp.net mvc -
i have following model classes
public class productviewmodel { public product products { get; set; } public ienumerable<product> lproducts { get; set; } } public partial class product { public string id { get; set; } public string detail { get; set; } }
then have following view both list , create
@model albaraka.models.productviewmodel <table class="table"> <tr> <th> id </th> <th> detail </th> </tr> @foreach (var obj in model.lproducts) { <tr> <td> @html.displayfor(modelitem => obj.id) </td> <td> @html.displayfor(modelitem => obj.detail) </td> </tr> } </table> <div> @using ()) { <div> <fieldset> <legend>account information</legend> <div class="editor-label"> @html.labelfor(m => m.id) </div> <div class="editor-field"> @html.textboxfor(m => m.products.id) @html.validationmessagefor(m => m.products.id) </div> <div class="editor-label"> @html.labelfor(m => m.products.detail) </div> <div class="editor-field"> @html.textboxfor(m => m.products.detail) @html.validationmessagefor(m => m.products.detail) </div> <p> <input type="submit" value="submit" /> </p> </fieldset> </div> } </div> @section scripts {}
this controller method list
[httpget] public viewresult product_list() { productviewmodel viewmodellist = new productviewmodel(); viewmodellist.lproducts = products in db.product products.details == "pending" select products; return view(viewmodellist.lproducts.tolist()); }
but here i'm getting following error
the model item passed dictionary of type 'system.collections.generic.list`1[projectname.models.product]', dictionary requires model item of type 'projectname.models.productviewmodel'.
the error self-explanatory, view accepts @model albaraka.models.productviewmodel
not @model ilist<albaraka.models.productviewmodel>
. in view using @model albaraka.models.productviewmodel
indicates model expected view of type productviewmodel
:
[httpget] public viewresult product_list() { productviewmodel viewmodellist = new productviewmodel(); viewmodellist.lproducts = (from products in db.ab_product products.details == "pending" select products).tolist(); return view(viewmodellist); }
Comments
Post a Comment