简体   繁体   中英

ASP.NET MVC : Displaying same results on different view

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?

Just pass the view name:

return View("Books", bookList);

...or....

return View("BookDetails", bookList);

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM