1

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?

3
  • "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." <- Why is that? Commented Mar 16, 2009 at 9:23
  • Ok I can create two Controller actions one for Detail List one for Normal List but I dont want to duplicate the same code in two places Commented Mar 16, 2009 at 9:45
  • create a method that retrieves the book data and call that from each action... Commented Mar 16, 2009 at 15:14

2 Answers 2

2

Just pass the view name:

return View("Books", bookList);

...or....

return View("BookDetails", bookList);
Sign up to request clarification or add additional context in comments.

Comments

1

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
<%}%>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.