9

I'm struggling myself here, to find a easy way to pass an array from the controller to the view on ASP.NET MVC framework.

so in my controller I would have something like:

public class HomeController : ApplicationController
{   
    public ActionResult Index()
    {
        string[] myArray = { "value01", "value02", "value03"};
        ViewData["passedArray"] = myArray;
        return View();
    }
}

so in my view I would have just a call to ViewData["passedArray"] and run a loop on it.

But apparently the ViewData is being received by the view as System.String, probably because of the declaration on the Array DataType, but unfortunately I don't know how to pass it properly and simply without create millions of code lines.

It would be fantastic if one could help me.

Thanks in advance

Jon Seigel
  • 12,251
  • 8
  • 58
  • 92
zanona
  • 12,345
  • 25
  • 86
  • 141

3 Answers3

16

You need to cast in the View

<% var myArray = (string[])ViewData["passedArray"]; %>
Trung Nguyen
  • 7,442
  • 2
  • 45
  • 87
Daniel Elliott
  • 22,647
  • 10
  • 64
  • 82
9

This should work by casting ViewData["passedArray"] within the view to string[]. Alternatively, if you want to go the extra mile: create a ViewModel class that contains this array as a member and pass that ViewModel to a strongly-typed version of your view.

David Andres
  • 31,351
  • 7
  • 46
  • 36
  • Thanks a lot David I used the casting as you said, it's perfect. But I like the idea to create a specific viewmodel to manage variables.I will try to inform myself better about it. Thanks again. – zanona Sep 10 '09 at 14:43
  • There are quite a few good reasons for opting for a ViewModel. (1) The need for a cast opens the door to runtime errors. (2) Creating a ViewModel gives you flexibility in the future to add features as you need them without having to change the type of the view. (3) Your code will slightly more readable without ViewData["key"] code littered in your view. – David Andres Sep 10 '09 at 15:01
  • David, so you mean if I create a new class for the controller like a MyCustomViewModel and pass this class like var myCustomOne = new MyCustomViewModel(); myCustomOne.myArray = new int[] {1,2,3,4} return View(myCustomOne); I would be able to access that array in my view like: Model.myArray as int[] is that right? – zanona Sep 11 '09 at 12:15
  • That's correct, though you may need to change the view definition for this to work. This will be easier to do if you copy the View markup you have, delete the view. Then right-click within the action method, select the Add View option, and make sure the Strongly typed option is checked and is set to your new ViewModel. – David Andres Sep 11 '09 at 12:29
0

You can use PartialView like this:

  • Controller

        public ActionResult Index()
        {
            List<string> arr = new List<string>() { "apple", "banana", "cat" };
            return View(arr);
        }
    
  • View

@model List<string>
@foreach (var item in Model) { 
        @Html.Partial("~/Views/Shared/Fruits/_myFruits.cshtml", item);
}
  • PatialView i.e. _myFruits.cshtml
@model  string
<li>@Model</li>
Israel Ocbina
  • 517
  • 8
  • 14