c# - Pull data from two linked tables to one View in MVC5 -
i have 2 tables associated many-to-many. menu table , ingredients table. want show ingredients based on menu item selected.
i created tables , made connect multiple multiple. created initializer , context.
inventoryinitializer.cs
using inventorymanagementsystem.models; using system; using system.collections.generic; using system.linq; using system.web; using static inventorymanagementsystem.models.ingredient; namespace inventorymanagementsystem.dal { public class inventoryinitializer : system.data.entity.dropcreatedatabasealways<inventorycontext> { protected override void seed(inventorycontext context) { var ingredients = new list<ingredient> { new ingredient {name = "cheese", quantity = 20, cost= 2.5, category = ingredienttype.dairy}, new ingredient {name = "ham", quantity = 15, cost= 4.0, category = ingredienttype.protein }, new ingredient {name = "lettuce", quantity = 12, cost = 2.0, category = ingredienttype.produce }, new ingredient {name = "wheat", quantity = 24, cost = 3.5, category = ingredienttype.bread } }; ingredients.foreach(s => context.ingredients.add(s)); context.savechanges(); var menuitems = new list<menuitem> { new menuitem { name = "ham , cheese", price = 9.0, ingredientlist = new list<ingredient> {ingredients.single(s=> s.name == "ham"), ingredients.single(s=> s.name == "cheese"), ingredients.single(s=> s.name == "lettuce"), ingredients.single(s=> s.name == "wheat") } }, new menuitem {name = "grilled cheese", price = 5.0, ingredientlist = new list<ingredient> {ingredients.single(s=> s.name == "cheese"), ingredients.single(s=> s.name == "wheat") } } }; menuitems.foreach(s => context.menuitems.add(s)); context.savechanges(); } } }
inventorycontext.cs
using inventorymanagementsystem.models; using system; using system.collections.generic; using system.data.entity; using system.data.entity.modelconfiguration.conventions; using system.linq; using system.web; namespace inventorymanagementsystem.dal { public class inventorycontext: dbcontext { public inventorycontext() : base("inventorycontext") { } public dbset<ingredient> ingredients { get; set; } public dbset<menuitem> menuitems { get; set; } protected override void onmodelcreating(dbmodelbuilder modelbuilder) { modelbuilder.conventions.remove<pluralizingtablenameconvention>(); } internal ingredient ingredient(func<object, bool> p) { throw new notimplementedexception(); } } }
then created controller inventorycontroller.cs
using system; using system.collections.generic; using system.data; using system.data.entity; using system.linq; using system.net; using system.web; using system.web.mvc; using inventorymanagementsystem.dal; using inventorymanagementsystem.models; namespace inventorymanagementsystem.controllers { public class inventorycontroller : controller { private inventorycontext db = new inventorycontext(); // get: inventory public actionresult index() { return view(db.ingredients.tolist()); } // get: inventory/details/5 public actionresult details(guid? id) { if (id == null) { return new httpstatuscoderesult(httpstatuscode.badrequest); } ingredient ingredient = db.ingredients.find(id); if (ingredient == null) { return httpnotfound(); } return view(ingredient); } // get: inventory/create public actionresult create() { return view(); } // post: inventory/create // protect overposting attacks, please enable specific properties want bind to, // more details see https://go.microsoft.com/fwlink/?linkid=317598. [httppost] [validateantiforgerytoken] public actionresult create([bind(include = "id,name,quantity,cost,category")] ingredient ingredient) { if (modelstate.isvalid) { ingredient.id = guid.newguid(); db.ingredients.add(ingredient); db.savechanges(); return redirecttoaction("index"); } return view(ingredient); } // get: inventory/edit/5 public actionresult edit(guid? id) { if (id == null) { return new httpstatuscoderesult(httpstatuscode.badrequest); } ingredient ingredient = db.ingredients.find(id); if (ingredient == null) { return httpnotfound(); } return view(ingredient); } // post: inventory/edit/5 // protect overposting attacks, please enable specific properties want bind to, // more details see https://go.microsoft.com/fwlink/?linkid=317598. [httppost] [validateantiforgerytoken] public actionresult edit([bind(include = "id,name,quantity,cost,category")] ingredient ingredient) { if (modelstate.isvalid) { db.entry(ingredient).state = entitystate.modified; db.savechanges(); return redirecttoaction("index"); } return view(ingredient); } // get: inventory/delete/5 public actionresult delete(guid? id) { if (id == null) { return new httpstatuscoderesult(httpstatuscode.badrequest); } ingredient ingredient = db.ingredients.find(id); if (ingredient == null) { return httpnotfound(); } return view(ingredient); } // post: inventory/delete/5 [httppost, actionname("delete")] [validateantiforgerytoken] public actionresult deleteconfirmed(guid id) { ingredient ingredient = db.ingredients.find(id); db.ingredients.remove(ingredient); db.savechanges(); return redirecttoaction("index"); } protected override void dispose(bool disposing) { if (disposing) { db.dispose(); } base.dispose(disposing); } } }
but cannot make ingredient items appear in table below menu items. want display in view line of menu item price , quantity below shows ingredients takes make menu item pricing.
@model ienumerable<inventorymanagementsystem.models.menuitem> @{ viewbag.title = "generatereport"; } <h2>inventory managment report</h2> <table class="table"> <tr> <th> @html.displaynamefor(model => model.name) </th> <th> @html.displaynamefor(model => model.price) </th> <th> @html.displaynamefor(model => model.quantitysold) </th> </tr> @foreach (var item in model) { <tr> <td> @html.displayfor(modelitem => item.name) </td> <td> @html.displayfor(modelitem => item.price) </td> <td> @html.displayfor(modelitem => item.quantitysold) </td> <dt> @html.displaynamefor(model => model.ingredientlist) </dt> <dd> <table class="table"> <tr> <th>ingredient</th> <th>cost</th> </tr> <tr> <td> @html.displayfor(modelitem => item.ingredient.name) </td> <td> @html.displayfor(modelitem => item.ingredient.price) </td> </tr> </table> </dd> <td> @html.actionlink("edit", "edit", new { id=item.id}) | @html.actionlink("delete", "delete", new { id=item.id}) </td> </tr> } </table>
wiki
Comments
Post a Comment