5

I'm working on a little project in which we have a table of engineers, a table of projects, and a table of elements. engineers are assigned multiple elements, and elements can have multiple projects. I was just wondering how I would go about showing all the elements a engineer is apart of.

Currently, I have a table created that associates a engineer with a element. it looks a little like this:

   [Engineer Elements]
[Engineer ID][Element ID]
   [1]           [2]
   [1]           [4]
   [2]           [2]
   [2]           [8]

So I do have a way to link the two tables. Could push me into the right direction on learning a bit more on linking these tables together using MVC?

p.s.w.g
  • 146,324
  • 30
  • 291
  • 331
Calvin
  • 710
  • 1
  • 10
  • 28

1 Answers1

8

If you don't already have a view model to represent this, just create one:

public class MyViewModel
{
    public Engineer Engineer { get; set; }
    public List<Element> Elements { get; set; }
}

Populate a set of view models in the 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 your view just specify that you're using a collection of view models:

@model List<MyViewModel>
@foreach(var vm in Model)
{
    <h1>Projects for engineer: @vm.Engineer.Name</ha>
    <ul>
    @foreach(var ele in vm.Elements)
    {
        <li>@ele.Name</li>
    }
    </ul>
}
p.s.w.g
  • 146,324
  • 30
  • 291
  • 331
  • Nice, I like it. One thing though, use the strong typed `List` and not `IEnumerable`, as looping can cause multiple enumerations. Change that and you got my +1 :) – Mathew Thompson Apr 05 '13 at 22:00
  • Both really, keep it consistent :) – Mathew Thompson Apr 05 '13 at 22:05
  • Woot! +1, good work :) – Mathew Thompson Apr 05 '13 at 22:06
  • @mattytommo I my *main* mvc project I did a fair amount of work to support very lazy loading of all my models - I almost always use `IQueryable` or `IEnumerable` and let the db queries run and materialize view models as the view demands, but that's not the typical setup. – p.s.w.g Apr 05 '13 at 22:10
  • 1
    Eek, that's pretty **bad practice**. As technically speaking, that could mean that database errors could happen at the point of your view. Models should be **fully loaded** before they are presented to the view strictly speaking. I use `List` because you **definitely know** that things have been evaluated before hitting the view. – Mathew Thompson Apr 05 '13 at 22:17
  • @mattytommo it works out pretty well for me. I mostly had to do it because of some very early design decisions. I've actually never had an issue with database issues in the view - maybe because I usually materialize a root view model in the controller. – p.s.w.g Apr 05 '13 at 22:26
  • Ah fair enough, whatever works for your scenario I guess! – Mathew Thompson Apr 05 '13 at 22:28
  • 1
    I tend to agree with @mattytommo. It may not cause problems having JIT queries, but explicitly making the things you need for your view load at the start keeps you honest. Though, it's still a matter of tastes and reasonable people can disagree. – Chris Pratt Apr 06 '13 at 01:28
  • Thanks! With a little modification on the controller, I was able to get what I wanted. Thank you very much! – Calvin Apr 08 '13 at 17:23