MVC4 ViewBag or ViewModel or?
Asked Answered
D

1

5

I need to send data in the form of a list for two different models in my database to a view in an MVC4 project.

Something like this:

Controller:

public ActionResult Index()
{
    Entities db = new Entities();

    ViewData["Cats"] = db.Cats.toList();
    ViewData["Dogs"] = db.Dogs.toList();

    return View();
}

View:

@* LIST ONE *@
<table>
    <tr>
        <th>
            @Html.DisplayNameFor(model => model.ListOneColOne)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.ListOneColTwo)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.ListOneColThree)
        </th>
    </tr>

@foreach (var item in @ViewData["Cats"]) {
    <tr>
        <td>
            @Html.DisplayFor(modelItem => item.ListOneColOne)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.ListOneColTwo)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.ListOneColThree)
        </td>
    </tr>


@* LIST TWO *@
<table>
    <tr>
        <th>
            @Html.DisplayNameFor(model => model.ListTwoColOne)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.ListTwoColTwo)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.ListTwoColThree)
        </th>
    </tr>

@foreach (var item in @ViewData["Dogs"]) {
    <tr>
        <td>
            @Html.DisplayFor(modelItem => item.ListTwoColOne)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.ListTwoColTwo)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.ListTwoColThree)
        </td>
    </tr>

The View is to display two list's, one list per model.

I'm unsure as to what the most efficient way to do this is?

Viewmodel?

Viewdata/Viewbag?

Something else?

(Please no third-party suggestions)

UPDATE:

Further I've attempted for over an hour now to implement the answer suggesting a List<T> Viewmodel without any luck. I believe this is due to the fact that my Viewmodel looks like this:

public class GalleryViewModel
{
    public Cat cat { get; set; }
    public Dog dog { get; set; }
}
Dag answered 1/5, 2013 at 5:46 Comment(0)
P
7

Try to explain your problem and your goal, so we know (specifically) what you're trying to do.

I take this to mean that you have two lists and you want to send them to a view. One way to do this is to put two lists into a model and send the model to the view, but you seem to have specified that you already have two models, so I'll go with that assumption.

Controller

public ActionResult Index()
{
    ModelA myModelA = new ModelA();
    ModelB myModelB = new ModelB();

    IndexViewModel viewModel = new IndexViewModel();

    viewModel.myModelA = myModelA;
    viewModel.myModelB = myModelB;

    return View(viewModel);
}

View Model

public class IndexViewModel
{
    public ModelA myModelA { get; set; }
    public ModelB myModelB { get; set; }
}

Model

public class ModelA
{
    public List<String> ListA { get; set; }
}

public class ModelB
{
    public List<String> ListB { get; set; }
}

View

@model IndexViewModel

@foreach (String item in model.myModelA)
{
    @item.ToString()
}

(Sorry if my C# is rusty)

Penetrating answered 1/5, 2013 at 6:14 Comment(2)
Thanks @row I've been trying to get this to work but I can't seem to figure it out. Apparently the issue is that my ViewModel aren't comprised of List<String> rather the actual model type e.g., public ListA listA { get; set; }. I'm going to update my initial post to reflect this.Dag
Took a while but I got it! Thank you Rowan.Dag

© 2022 - 2024 — McMap. All rights reserved.