tags:

views:

379

answers:

2

I am developing a learning project with ASP.NET MVC.I have a page that lists logged users's books and I want to display books in two formats like this

  • Normal List --> Display Book Thumbnail, Title,Page count,Author...

  • Detail List ---> Display only Book Title,Author,Page Count in HTML Table format
    format

so I have two view pages Books.aspx,BookDetails.aspx. One for normal list,one for detail list but I have one controller action that returns books from database and can return results only to one page.

    public ActionResult Index()
    {
        //get books from database
        return View(bookList);
    }

Do I have to include a parameter and check parameter to return list to different view or is there a better way to do this? How can I use same Controller action to display two views?

+1  A: 

Look into using partial views and create controller actions for them

public ActionResult NormalList{
    ViewData["normalList"] = //db retrieval code;
    return View("NormalList");
}

public ActionResult DetailedList{
    ViewData["detailedList"] = //db retrieval code;
    return View("DetailedList");
}

in your page

<%= Html.RenderPartial("NormalList", ViewData)%>


<%= Html.RenderPartial("DetailedList", ViewData)%>

and in your partial

<%foreach(var item in (IEnumerable)ViewData["normalList"]){%>
//blah blah blah
<%}%>
Ayo
+1  A: 

Just pass the view name:

return View("Books", bookList);

...or....

return View("BookDetails", bookList);
Craig Stuntz