Balu
Balu

Reputation: 543

How to pass List from Controller to View in MVC 3

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.

Upvotes: 41

Views: 185552

Answers (5)

Gaurav Nandankar
Gaurav Nandankar

Reputation: 21

Here's Some ways to pass model list or data from controller to view in c# .net mvc

The First One using SelectListItem

Controller :

[HttpGet]
   public IActionResult Create()
   {
       IEnumerable<SelectListItem> list = context.Villas.ToList().Select(n => new SelectListItem
       {
           Text = n.Name,
           Value = n.Id.ToString()
       });

       ViewData["VillaList"] = list;

       return View();
   }

View :

<div class="form-floating py-1 col-12">
     <select asp-for="@Model.VillaId" asp-items="@ViewData["VillaList"] as IEnumerable<SelectListItem>" class="form-control border shadow" >
             <option disabled selected> Select Villa </option>
     </select>
     <label asp-for="VillaId" class="ms-2"></label>
     <span asp-validation-for="VillaId" class="text-danger"></span>
 </div>

The Second One using ViewBag for dynamic data

Controller :

[HttpGet]
  public IActionResult Create()
  {
      IEnumerable<SelectListItem> list = context.Villas.ToList().Select(n => new SelectListItem
      {
          Text = n.Name,
          Value = n.Id.ToString()
      });

      ViewBag.VillaList = list;

      return View();
  }

View :

<div class="form-floating py-1 col-12">
    <select asp-for="@Model.VillaId" asp-items="@ViewBag.VillaList" class="form-control border shadow">
            <option disabled selected> Select Villa </option>
    </select>
    <label asp-for="VillaId" class="ms-2"></label>
    <span asp-validation-for="VillaId" class="text-danger"></span>
</div>

Upvotes: 0

Zafor
Zafor

Reputation: 353

  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.

Upvotes: 7

Nelson Miranda
Nelson Miranda

Reputation: 5564

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

Upvotes: 14

archil
archil

Reputation: 39501

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>
}

Upvotes: 55

Dennis Traub
Dennis Traub

Reputation: 51684

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) { ... }

Upvotes: 9

Related Questions