mysql - ASP.NET MVC 4, multiple models in one view? -
this question has answer here:
- multiple models in view 9 answers
i'm working on little project in have table of engineers, table of projects, , table of elements. engineers assigned multiple elements, , elements can have multiple projects. wondering how go showing elements engineer apart of.
currently, have table created associates engineer element. looks little this:
[engineer elements] [engineer id][element id] [1] [2] [1] [4] [2] [2] [2] [8] so have way link 2 tables. push me right direction on learning bit more on linking these tables using mvc?
if don't have view model represent this, create one:
public class myviewmodel { public engineer engineer { get; set; } public list<element> elements { get; set; } } populate set of view models in controller
public actionresult myaction() { var viewmodels = (from e in db.engineers select new myviewmodel { engineer = e, elements = e.elements, }) .tolist(); return view(viewmodels); } and in view specify you're using collection of view models:
@model list<myviewmodel> @foreach(var vm in model) { <h1>projects engineer: @vm.engineer.name</ha> <ul> @foreach(var ele in vm.elements) { <li>@ele.name</li> } </ul> }
Comments
Post a Comment