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

4

5

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 which I need to access in a View and iterate through.

Thanks in advance.

Possess answered 24/11, 2008 at 18:11 Comment(3)
is there a microsoft product called mvc? if so, shouldn't we fight against (again) using generic words as a product name? don't let it go down like 'word', 'sql' and 'windows'Eckman
Please make your question a little more descriptive re: your actual scenario. "I'm having issues passing stuff around" is not helpful for those answering and it is not helpful for those that may find your question in the future.Firstrate
Yeah sorry I did use ASP.NET MVC in the title but not in the body of my post.Possess
C
10

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:

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:

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)

<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.

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.

Catarrhine answered 24/11, 2008 at 19:18 Comment(5)
thanks a lot, everyone was helpful but this one helped me the mostPossess
Should ViewPage<string> be ViewPage<List<string>> ?Jason
Yeah Patrik it should, but I figured that!Possess
Ah, sorry about that. Glad you figured it out.Catarrhine
@Catarrhine How to make the view to extend from ViewPage<string> when it is html file?Arand
H
4

The quick and dirty way is to pass it via 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>
Hellenhellene answered 24/11, 2008 at 18:53 Comment(0)
S
2

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.

Surpass answered 24/11, 2008 at 18:33 Comment(0)
I
1

If I'm not mistaken, the repeater control requires the page model. (Page model being what classic ASP.NET uses)

But you should take a look at this link: http://haacked.com/archive/2008/05/03/code-based-repeater-for-asp.net-mvc.aspx

Insincere answered 24/11, 2008 at 18:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.