In ASP.Net MVC I would like to render a different partial view depending on the renderview query string parameter.
在ASP.Net MVC中,我想根据renderview查询字符串参数呈现不同的局部视图。
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>
3 个解决方案
#1
6
Your View Should be something like:
你的观点应该是这样的:
<%@ Page Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<Models.MyModel>" %>
Then in MyModel
然后在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.
理想情况下,View所需的所有属性或参数或数据都应该是Model的一部分。
I hope it helps.
我希望它有所帮助。
#2
3
An alternative approach would be to use Restful Urls to invoke the appropriate controller action and view.
另一种方法是使用Restful Urls来调用适当的控制器操作和视图。
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:
并对应于ProductController方法:
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}
#3
0
Paul's method is good, but if you decide you want to pass the int, you need to create a view model.
Paul的方法很好,但是如果你决定要传递int,你需要创建一个视图模型。
In your controller add this
在你的控制器中添加它
public class ProductsFormViewModel
{
// Properties
public Products Products { get; private set; }
public int? Renderview { get; private set; }
// Constructor
public ProductsFormViewModel(Products p_products, int? p_renderView)
{
Products = p_products;
Renderview = renderView;
}
}
Then pass this into the view
然后将其传递到视图中
return View(new ProductsFormViewModel(products, renderview);
And then in the view
然后在视图中
Inherits="System.Web.Mvc.ViewPage<YourNamespace.Controllers.ProductsFormViewModel>"
#1
6
Your View Should be something like:
你的观点应该是这样的:
<%@ Page Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<Models.MyModel>" %>
Then in MyModel
然后在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.
理想情况下,View所需的所有属性或参数或数据都应该是Model的一部分。
I hope it helps.
我希望它有所帮助。
#2
3
An alternative approach would be to use Restful Urls to invoke the appropriate controller action and view.
另一种方法是使用Restful Urls来调用适当的控制器操作和视图。
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:
并对应于ProductController方法:
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}
#3
0
Paul's method is good, but if you decide you want to pass the int, you need to create a view model.
Paul的方法很好,但是如果你决定要传递int,你需要创建一个视图模型。
In your controller add this
在你的控制器中添加它
public class ProductsFormViewModel
{
// Properties
public Products Products { get; private set; }
public int? Renderview { get; private set; }
// Constructor
public ProductsFormViewModel(Products p_products, int? p_renderView)
{
Products = p_products;
Renderview = renderView;
}
}
Then pass this into the view
然后将其传递到视图中
return View(new ProductsFormViewModel(products, renderview);
And then in the view
然后在视图中
Inherits="System.Web.Mvc.ViewPage<YourNamespace.Controllers.ProductsFormViewModel>"