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

前端 未结 4 2150
太阳男子
太阳男子 2020-12-30 15:25

How do I do the above? I\'ve started using MVC and I\'m having issues passing data around.

My specific problem is to do with a list of objects I have in my Model whi

4条回答
  •  萌比男神i
    2020-12-30 16:21

    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:

    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("Title")%>
    

    Hope that helps.

提交回复
热议问题