asp.net mvc - Mvc 5 - 2 model in view -
i started mvc , i'm trying learn how create order , order details project.
i have inventory contains items in there facing problems trying pull data out inventory through order details.
how combine 2 together? @model inventorytest.models.inventory.order , @model ienumerable< inventorytest.models.inventory.inventories> in view code?
i apologies messy structure of code i'm still learning hope advice me on problem i'm facing.
inventory model:
public int inventoryid { get; set; } public string itemno { get; set; } public string item { get; set; } public int quantity { get; set; }
order model:
public int orderid { get; set; } public datetime date { get; set; } public int employeeid { get; set; } public int departmentid { get; set; } public ienumerable<selectlistitem> getemployee() { var query = db.employees.select(c => new selectlistitem { value = c.employeeid.tostring(), text = c.displayname, }); return query.asenumerable(); } public ienumerable<selectlistitem> getdeptlist() { var query = db.departments.select(c => new selectlistitem { value = c.departmentid.tostring(), text = c.description, }); return query.asenumerable(); }
order detail model:
public int orderdetailid { get; set; } public int quantity { get; set; } public int orderid { get; set; } public int inventoryid { get; set; }
on view code order create follows:
@model inventorytest.models.inventory.order @{ viewbag.title = "order forms"; } <h2>order forms</h2> @using (html.beginform()) { @html.antiforgerytoken() <div class="form-horizontal"> <hr /> @html.validationsummary(true, "", new { @class = "text-danger" }) <div class="row"> <div class="col-sm-4"> @html.labelfor(model => model.employeeid, htmlattributes: new { @class = "control-label col-md-2" }) <div class="col-md-offset-4"> @html.validationmessagefor(model => model.employeeid, "", new { @class = "text-danger" }) @html.dropdownlistfor(m => m.employeeid, model.getemployee(), "please select", new { @style = "width: 200px;height:35px", @class = "input-select", @data_bv_notempty = "true", @data_bv_message = "please select project." }) </div> </div> <div class="col-sm-4"> @html.labelfor(model => model.department, htmlattributes: new { @class = "control-label col-md-2" }) <div class="col-md-offset-4"> @html.validationmessagefor(model => model.departmentid, "", new { @class = "text-danger" }) @html.dropdownlistfor(m => m.departmentid, model.getdeptlist(), "please select", new { @style = "width: 200px;height:35px", @class = "input-select", @data_bv_notempty = "true", @data_bv_message = "please select project." }) </div> </div> </div> </div> <hr /> } <br /> <h4>item listing</h4> <table class="table table-hover"> <tr> <th>s/n</th> <th>item no.</th> <th>item</th> <th>quantity</th> </tr> *//i want use foreach populate data here* <tr> <td></td> <td></td> <td></td> <td></td> <td></td> </tr> </table> <div> @html.actionlink("back list", "index") </div> @section scripts { @scripts.render("~/bundles/jqueryval") }
create view model class:
public class orderandinventoryviewmodel { public order orderinfo { get; set; } public ienumberable<inventories> inventoryinfo { get; set; } }
and in view, use:
@model <your namespace goes here>.orderandinventoryviewmodel
assign appropriate order , inventory information orderandinventory object in controller, , pass view.