Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MVC4 ViewBag or ViewModel or?

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; }
}
like image 866
id.ot Avatar asked May 01 '13 05:05

id.ot


People also ask

Which one is better ViewBag or ViewData?

ViewData is a dictionary object and it is property of ControllerBase class. ViewData is faster than ViewBag .

Is ViewData faster than ViewBag in MVC?

ViewBag will be slower than ViewData; but probably not enough to warrant concern.

Should you use ViewBag?

Because ViewBag is a property of ControllerBase , it provides a convenient place for storing key/value pairs during controller operations, using a first-class syntax. You would use it anytime you need to send key/value pairs to a View, and you want the convenience and clean syntax of a dynamic dictionary.

Is there any use of ViewModel of MVC?

What ViewModel is. In ASP.NET MVC, ViewModels are used to shape multiple entities from one or more models into a single object. This conversion into single object provides us better optimization. You can see the concept of ViewModel in the image below.


1 Answers

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)

like image 121
Rowan Freeman Avatar answered Sep 28 '22 00:09

Rowan Freeman