38

I have a List<> binded with some data in Controller action and I want to pass that List<> to View to bind with DataGrid in Razor View.

I am new to MVC.Can any one help me how to pass and how to access in View.

demonplus
  • 5,613
  • 12
  • 49
  • 68
Balu
  • 513
  • 1
  • 5
  • 9
  • Summing up the answers so far. You can use for this: 1 - Using `View()`, 2 - Using `ViewData`, 3 - Using `ViewBag`, 4 - Using your custom `class`, and 5 - Some combinations of those approaches. – carloswm85 Jul 01 '22 at 12:47

4 Answers4

51

Passing data to view is simple as passing object to method. Take a look at Controller.View Method

protected internal ViewResult View(
    Object model
)

Something like this

//controller

List<MyObject> list = new List<MyObject>();

return View(list);


//view

@model List<MyObject>

// and property Model is type of List<MyObject>

@foreach(var item in Model)
{
    <span>@item.Name</span>
}
archil
  • 39,013
  • 7
  • 65
  • 82
  • 5
    Worked for me when I had `@Model` instead of `@model`. – jonas Aug 07 '14 at 21:36
  • what if you want to send 2 separate lists from controller to view? – Jogi Feb 27 '17 at 17:21
  • 2
    Than you should create ViewModel class that has two List properties and pass instance of that class to view – archil Feb 28 '17 at 07:10
  • is there are any way for fetch data without foreach loop and simply we write model => model.Data @archil –  Nov 08 '17 at 13:54
14

I did this;

In controller:

public ActionResult Index()
{
  var invoices = db.Invoices;

  var categories = db.Categories.ToList();
  ViewData["MyData"] = categories; // Send this list to the view

  return View(invoices.ToList());
}

In view:

@model IEnumerable<abc.Models.Invoice>

@{
    ViewBag.Title = "Invoices";
}

@{
  var categories = (List<Category>) ViewData["MyData"]; // Cast the list
}

@foreach (var c in @categories) // Print the list
{
  @Html.Label(c.Name);
}

<table>
    ...
    @foreach (var item in Model) 
    {
      ...
    }
</table>

Hope it helps

Nelson Miranda
  • 5,484
  • 5
  • 33
  • 54
  • what if you want to send 2 separate lists to View from Controller? – Jogi Feb 27 '17 at 21:36
  • 2
    Hi @RehanKhan, I think you should you make then 2 viewdata with 2 different names. ViewData["MyList1"] and ViewData["MyList2"] Best regards. – Nelson Miranda Jul 04 '17 at 14:27
9

You can use the dynamic object ViewBag to pass data from Controllers to Views.

Add the following to your controller:

ViewBag.MyList = myList;

Then you can acces it from your view:

@ViewBag.MyList

// e.g.
@foreach (var item in ViewBag.MyList) { ... }
Dennis Traub
  • 50,557
  • 7
  • 93
  • 108
6
  1. Create a model which contains your list and other things you need for the view.

    For example:

    public class MyModel
    {
        public List<string> _MyList { get; set; }
    }
    
  2. From the action method put your desired list to the Model, _MyList property, like:

    public ActionResult ArticleList(MyModel model)
    {
        model._MyList = new List<string>{"item1","item2","item3"};
        return PartialView(@"~/Views/Home/MyView.cshtml", model);
    }
    
  3. In your view access the model as follows

    @model MyModel
    foreach (var item in Model)
    {
       <div>@item</div>
    }
    

I think it will help for start.

Mohsen Sichani
  • 1,002
  • 12
  • 33
Zafor
  • 347
  • 2
  • 8