asp.net-mvc 如何在 MVC 3 中将列表从控制器传递到视图
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10994790/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to pass List from Controller to View in MVC 3
提问by Balu
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.
我有一个 List<> 与 Controller 操作中的一些数据绑定,我想将该 List<> 传递给 View 以与 Razor View 中的 DataGrid 绑定。
I am new to MVC.Can any one help me how to pass and how to access in View.
我是 MVC 的新手。任何人都可以帮助我如何通过以及如何在 View 中访问。
回答by archil
Passing data to view is simple as passing object to method. Take a look at Controller.View Method
将数据传递给视图就像将对象传递给方法一样简单。看一下 Controller.View 方法
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>
}
回答by Nelson Miranda
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
希望能帮助到你
回答by Dennis Traub
You can use the dynamic object ViewBagto pass data from Controllers to Views.
您可以使用动态对象ViewBag将数据从控制器传递到视图。
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) { ... }
回答by Abu Zafor
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; } }From the action method put your desired list to the Model,
_MyListproperty, like:public ActionResult ArticleList(MyModel model) { model._MyList = new List<string>{"item1","item2","item3"}; return PartialView(@"~/Views/Home/MyView.cshtml", model); }In your view access the model as follows
@model MyModel foreach (var item in Model) { <div>@item</div> }
创建一个模型,其中包含您的列表和视图所需的其他内容。
例如:
public class MyModel { public List<string> _MyList { get; set; } }从 action 方法将您想要的列表放到 Model
_MyList属性中,例如:public ActionResult ArticleList(MyModel model) { model._MyList = new List<string>{"item1","item2","item3"}; return PartialView(@"~/Views/Home/MyView.cshtml", model); }在您的视图中访问模型如下
@model MyModel foreach (var item in Model) { <div>@item</div> }
I think it will help for start.
我认为这将有助于开始。

