Mason240
Mason240

Reputation: 3044

Best method to pass what amounts to an associative array from a controller to a view in MVC 4

I have 2 arrays that I need to pass to and display in a view. Right now I have:

String[] unactivePanelsHeader = new String[] {"GT5C", "GT5E", "GT5P", "GT5W"};
int[] unactivePanelsValues = new int[4];

//Values for unactivePanelsValues created with a linq query

ViewBag.unactivatedHeader = unactivePanelsHeader;
ViewBag.unactivatedPanels = unactivePanelsValues;

The results are then displayed in the view.

<table>
    <tr>
    @for (int i = 0; i < 4; i++ )
    {
        <td>@ViewBag.unactivatedHeader[i]</td>
    }
    </tr>
    <tr>
    @for (int i = 0; i < 4; i++ )
    {
        <td>@ViewBag.unactivatedPanels[i]</td>
    }
    </tr>

It works, but I am new to this platform and seems like there should be a better way.

Upvotes: 0

Views: 737

Answers (4)

Syneryx
Syneryx

Reputation: 1288

There are several ways to pass along and display data.

One of them being a strongly typed model as mentioned by Sruly.

I would advise you to follow or look at these tutorials http://www.asp.net/mvc/tutorials/mvc-4/getting-started-with-aspnet-mvc4/intro-to-aspnet-mvc-4

public ActionResult ViewUnactivated()
    {
        var unactivatedViewModel= new UnactivatedViewModel{ UnactivatedHeaders = unactivatedPanelsHeaders, UnactivatedPanels = unactivatedPanelsValues };
        return View(UnactivatedViewModel);
    }

And create a corresponding view where you loop over the model property's

Upvotes: 1

Dirk Trilsbeek
Dirk Trilsbeek

Reputation: 6033

The Viewbag is primarily interesting for generic views, whenever you don't know what type of data you are going to display. Views that display information whose type you know in advance should be created as strongly typed views. You create a model with properties for all your view data, create an instance of that model in your controller and pass the model on to your view. For instance, a model could look like this:

class MyModel
{
    IList<string> UnactivatedHeaders { get; set; }
    IList<int> UnactivatedPanels { get; set; }


    public MyModel
    {
        UnactivatedHeaders = new List<string>();
        UnactivatedPanels = new List<int>();
    }
}

your controller would then call your view like this:

public ActionResult Index()
{
    var model = new MyModel();
    model.UnactivatedHeaders.Add("GT5C");
    // and so on...

    return View(model);

}

and in your view, you will be able to access the properties of your model like this:

@foreach(var header in Model.UnactivatedHeaders) {
<td>@header</td>
}

to create a strongly typed view, first create your model and compile the project at least once. Then create your view and mark the checkbox "create strongly typed view" and select your model class in the dropdown list below.

Upvotes: 1

Andrew Kelly
Andrew Kelly

Reputation: 123

The 2 arrays should be contained within the model that is returned at the top of the page.

E.g. Inherits="ViewPage<>"

The other option is to pass the arrays in a PartialView

E.g. Html.RenderPartial("YourControl", new Model { x = "value1", y = "value2" } );

Upvotes: 1

Sruly
Sruly

Reputation: 10550

It is better to create a strongly typed model class that includes all the data needed for the view.

Then you would use

@Model.Property

Upvotes: 1

Related Questions