In ASP.Net MVC I would like to render a different partial view depending on the renderview query string parameter.
Therefore providing the facility for the user to choose to view products by thumbnail or by details.
I have access to the chosen parameter in the controller but I do not know how to or, if I should be passing this to the view along with the products list so the view can implement the logic for deciding which partial view to display?
public ActionResult Products(string id, int? renderview)
{
var products = productRepository.GetProducts(id).ToList();
return View("Products", products);
}
<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<IEnumerable<MLBWebRole.Models.Product>>" %>
<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
Products
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<h2>Products</h2>
<p>This is the Products page</p>
<p><a href="?renderview=0">thumbnails</a> <a href="?renderview=1">details</a></p>
<% if (renderview == 1)
{%>
<% Html.RenderPartial("ProductsDetailList"); %>
<% }
else
{ %>
<% Html.RenderPartial("ProductsThumbnailList"); %>
<% } %>
</asp:Content>
You should create a ViewModel class (just a . cs class) that contains all of the things you require on the page. Then the first line of the View, you should use the viewmodel class as your model. Then you'll be able to access everything on the page.
Your View Should be something like:
<%@ Page Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<Models.MyModel>" %>
Then in MyModel
Expose Property:
public bool RenderDetailView {get;set;}
In your controller action:
public ActionResult Products(string id, int? renderview)
{
var products = productRepository.GetProducts(id).ToList();
return View("Products", new MyModel {RenderDetailView = renderview.HasValue});
}
Then in your view, make check like:
<% if (Model.RenderDetailView)
Ideally, all the properties or parameters or data which a View needs in order to present itself should be part of Model.
I hope it helps.
An alternative approach would be to use Restful Urls to invoke the appropriate controller action and view.
This makes the urls reflect what you are seeing on the screen and makes the design more extensible; should you need to add other views of the data in the future (summary, latest, etc) you add the new view, no need for partials unless the main body of the view gets more complicated and has to be factored out to a partial view.
The URLs would look like:
~/product/1/detail
~/product/1/thumbnail
And correspond to ProductController methods:
public ActionResult Detail(String id)
{
var products = productRepository.GetProducts(id).ToList();
return View("Detail", products);
}
public ActionResult Thumbnail(string id)
{
var products = productRepository.GetProducts(id).ToList();
return View("Thumbnail", products);
}
You enable the routing with a route like:
{controller}/{id}/{action}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With