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
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
@{
ViewBag.Title = "Invoices";
}
@{
var categories = (List) ViewData["MyData"]; // Cast the list
}
@foreach (var c in @categories) // Print the list
{
@Html.Label(c.Name);
}
...
@foreach (var item in Model)
{
...
}
Hope it helps