C# ASP.NET MVC:如何将列表(从模型中的类)传递给视图中的中继器?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/314933/
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
ASP.NET MVC: How do I pass a list (from a class in Model) to a repeater in a View?
提问by Tablet
How do I do the above? I've started using MVC and I'm having issues passing data around.
我如何做到以上几点?我已经开始使用 MVC,但在传递数据时遇到了问题。
My specific problem is to do with a list of objects I have in my Model which I need to access in a View and iterate through.
我的具体问题是与我的模型中的对象列表有关,我需要在视图中访问这些对象并进行迭代。
Thanks in advance.
提前致谢。
采纳答案by Rune
Let's say your controller action looks something like
假设您的控制器操作看起来像
public ActionResult List()
{
List<string> myList = database.GetListOfStrings();
(...)
}
Now you want to pass your list to the view, say "List.aspx". You do this by having the action return a ViewResult (ViewResult is a subclass of ActionResult). You can use the Controller's View method to return a ViewResult like so:
现在你想把你的列表传递给视图,说“List.aspx”。您可以通过让操作返回 ViewResult(ViewResult 是 ActionResult 的子类)来实现此目的。您可以使用控制器的 View 方法返回一个 ViewResult ,如下所示:
public ActionResult List()
{
List<string> myList = database.GetListOfStrings();
(...)
return View("List", myList);
}
To be able to access the list in a strongly-typed fashion in your view, it must derive from ViewPage, where T is the type of the data you are passing in. Thus, in the current case our view (in List.aspx.cs) would something like this:
为了能够在您的视图中以强类型方式访问列表,它必须从 ViewPage 派生,其中 T 是您传入的数据类型。因此,在当前情况下,我们的视图(在 List.aspx. cs) 会是这样的:
public partial class List : ViewPage<string>
{
(...)
}
The data passed into the view in this way is referred to as the "ViewData". To access the data, you must go through the ViewData.Model properties on the ViewPage. Thus, to render the contents of the list you would write (in List.aspx)
以这种方式传入视图的数据称为“ViewData”。要访问数据,您必须通过 ViewPage 上的 ViewData.Model 属性。因此,要呈现您将编写的列表内容(在 List.aspx 中)
<ul>
<% foreach(var s in this.ViewData.Model){ %>
<li> <%= s %> </li>
<% } %>
</ul>
Here, this.ViewData.Model has the type you specified the type parameter T in ViewPage, so in our case this.ViewData.Model has type List.
在这里,this.ViewData.Model 具有您在 ViewPage 中指定的类型参数 T 的类型,因此在我们的示例中 this.ViewData.Model 具有类型 List。
You canuse a repeater for rendering stuff like this, but I wouldn't recommend it. If you want to use something similar, check out the Grid module of the MvcContrib project on CodePlex.
你可以使用中继器来渲染这样的东西,但我不推荐它。如果您想使用类似的东西,请查看 CodePlex 上 MvcContrib 项目的 Grid 模块。
回答by TimothyP
If I'm not mistaken, the repeater control requires the page model. (Page model being what classic ASP.NET uses)
如果我没记错的话,转发器控件需要页面模型。(页面模型是经典的 ASP.NET 使用的)
But you should take a look at this link: http://haacked.com/archive/2008/05/03/code-based-repeater-for-asp.net-mvc.aspx
但你应该看看这个链接:http: //haacked.com/archive/2008/05/03/code-based-repeater-for-asp.net-mvc.aspx
回答by Dave Weaver
ASP.NET MVC has a couple ways to pass data to your View. The primary way of passing your model classes to the View is to include it in the returned ViewResult class from your controller, like below:
ASP.NET MVC 有几种方法可以将数据传递到您的视图。将模型类传递给视图的主要方法是将其包含在从控制器返回的 ViewResult 类中,如下所示:
Function List() As ViewResult
' pass other information in the viewdata dictionary
ViewData("Title") = "All Items"
' get our item list from the Model classes
Dim items = Model.ItemRepository.GetAllItems()
' return as part of result
Return View(items)
End Function
Then from within your view you can access that list like below:
然后从您的视图中,您可以访问该列表,如下所示:
<% For Each item In ViewData.Model %>
<%=item.Name%>
<% End If %>
The other method of passing data is thru the ViewData dictionary as shown in the controller function above. You can access that from within your view like:
另一种传递数据的方法是通过 ViewData 字典,如上面的控制器函数所示。您可以从您的视图中访问它,例如:
<%=ViewData("Title")%>
Hope that helps.
希望有帮助。
回答by Todd Smith
The quick and dirty way is to pass it via ViewData
快速而肮脏的方法是通过 ViewData 传递它
public ActionResult List()
{
ViewData["MyList"] = new List<string> () {"test1", "test2"};
return View ();
}
then you can access it in your view
然后你可以在你的视图中访问它
<ul>
<% foreach (string item in (List<string>)ViewData["MyList"]) { %>
<li><%= item %></li>
<% }%>
</ul>