0

I have a cshtml view linked to a controller for a model for one table in my database, but I need to do a for each loop that references a second model for a second table. Since setting 2 models at the top of the page is not possible foreach (var row in model) won't work.

what I need is something like this

@model MVCapp.Models.COOKIE
@using MVCapp.Models;

@{
  ViewBag.Title = "Update";
  ;

  foreach (var row in IEnumerable<MVCapp.Models.COOKIE_JARS)
  {
     some code 
  }
}

Update:
Although that is not allowed because I'm using a type like a variable. So what I am asking is if there is a way to set type:IEnumerable<MVCapp.Models.COOKIE_JARS to a variable or a second model, Or if there is a completely alternative method to achieve this looping through a second database table that I am trying to attempt.

6
  • 3
    Create a viewmodael that has 2 properties that encapsulate your 2 models or make your model a Tuple<Model1,Model2>. Commented Oct 31, 2014 at 14:00
  • Oh using the Tuple as a model, never thought of that. It's a bit dirty, I like that :). Commented Oct 31, 2014 at 14:11
  • Using Tuple<Model,Model2> method I get the does not contain public definition for GetEnumerator Using IEnumerable<Tuple<Model1,Model2>> Does not seem to fix this as well as Tuple<IEnumerable<Model>,IEnumerable<Model2> Commented Oct 31, 2014 at 14:46
  • I fixed this by changing Model to Model.Item1 in my foreach loop. Commented Oct 31, 2014 at 14:59
  • 1
    @RyeNyeTheWebSiteGuy Honestly, I wouldn't recommend using that technique, you'll be a lot better off doing a ViewModel. Using the Tuple method is quite dirty, what would happen if you would need info from a third Model? Commented Oct 31, 2014 at 16:55

1 Answer 1

2

The best way to do this is to create a ViewModel, a class that will contain the information of both your models.

You view must never made any logic or get information from your database, this is the goal of the Controller.

So, the idea is that your controller will make the queries, and add the data into your ViewModel object that will then be sent to the View.

Example :

Your ViewModel:

public class CookieEatersViewModel
{
    public IEnumerable<Cookie> Cookies {get; set;}
    public IEnumerable<Monster> Monsters {get; set;}
}

Your View:

@model MVCApp.ViewModels.CookieEatersViewModel

<h2>Update</h2>

<p>Use the informations from your ViewModel here</p>

Your controller:

public ActionResult Index()
{
    CookieEatersViewModel cevm = new CookieEatersViewModel();
    cevm.Cookies = repo.GetCookies();
    cevm.Monsters = repo.GetMonsters();

    return View(vm);
} 
Sign up to request clarification or add additional context in comments.

3 Comments

What does the "repo" do, I get an error on it, is there an assembly i need to add to the top of the file for it to work?
"repo" was just a sample name for what you'd call your repository see msdn.microsoft.com/en-us/library/ff649690.aspx. It's just a sample to say "get your data from whatever source you have".
Okay I see, I am very new to developing as specially with MVC and C# so I know very little. Thank you for you help.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.