简体   繁体   English

ASP.NET MVC:如何将列表(从Model中的类)传递到View中的转发器?

[英]ASP.NET MVC: How do I pass a list (from a class in Model) to a repeater in a View?

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. 我的具体问题是我的模型中的对象列表,我需要在View中访问并迭代。

Thanks in advance. 提前致谢。

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". 现在您要将列表传递给视图,例如“List.aspx”。 You do this by having the action return a ViewResult (ViewResult is a subclass of ActionResult). 您可以通过让操作返回ViewResult(ViewResult是ActionResult的子类)来完成此操作。 You can use the Controller's View method to return a ViewResult like so: 您可以使用Controller的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". 以这种方式传递到视图中的数据称为“ViewData”。 To access the data, you must go through the ViewData.Model properties on the ViewPage. 要访问数据,您必须浏览ViewPage上的ViewData.Model属性。 Thus, to render the contents of the list you would write (in List.aspx) 因此,要渲染列表的内容,您将编写(在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 can use 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模块。

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>

ASP.NET MVC has a couple ways to pass data to your View. ASP.NET MVC有几种方法可以将数据传递给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: 将模型类传递给View的主要方法是将它包含在控制器返回的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. 传递数据的另一种方法是通过ViewData字典,如上面的控制器函数所示。 You can access that from within your view like: 您可以在视图中访问它,例如:

<%=ViewData("Title")%>

Hope that helps. 希望有所帮助。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在ASP.NET MVC 4中显示从模型到另一个视图的列表 - How do i show a list from a model to another View in ASP.NET MVC 4 如何将Enum从视图传递到模型ASP.Net MVC - How to pass Enum from view to model ASP.Net MVC 如何在ASP.NET MVC 3中为填充的下拉列表创建视图模型 - How do I create a view model for a populated drop down list in ASP.NET MVC 3 传递列表值以查看ASP.NET MVC中的模型类变量 - Pass list value to view model class variable in ASP.NET MVC 如何将DateTime参数从View传递到ASP.net MVC5中的Controller? - How do I pass DateTime parameters from View to Controller in ASP.net MVC5? 如何将URL从XML来源传递到asp.net中继器? - How do I pass a url into an asp.net repeater from an XML source? ASP.Net MVC 4如何将模型属性传递到视图中? - ASP.Net MVC 4 How to pass model property into view? 如何在ASP.NET MVC中传递多个模型? - How to pass multiple model in view in ASP.NET MVC? C#-如何在ASP.NET MVC的HTML表单中传递内部带有其他模型的模型 - C# - How do I pass a model with another model inside from a HTML form in ASP.NET MVC 如何在ASP.NET MVC的父域模型中将此子模型引用传递给虚拟ICollection? - How do I pass this child model reference to a virtual ICollection in parent domain model in ASP.NET MVC?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM