ASP.Net MVC 将多个参数传递给视图
在 ASP.Net MVC 中,我想根据 renderview 查询字符串参数呈现不同的部分视图。
因此,为用户提供了选择通过缩略图或详细信息查看产品的便利。
我可以访问控制器中选定的参数,但我不知道如何访问,或者是否应该将其与产品列表一起传递给视图,以便视图可以实现决定显示哪个部分视图的逻辑?
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>
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>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您的视图应该类似于:
然后在 MyModel 中
公开属性:
在您的控制器操作中:
然后在您的视图中,进行如下检查:
理想情况下,视图展示自身所需的所有属性、参数或数据都应该是模型的一部分。
我希望它有帮助。
Your View Should be something like:
Then in MyModel
Expose Property:
In your controller action:
Then in your view, make check like:
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.
另一种方法是使用 Restful URL 来调用适当的控制器操作和视图。
这使得 url 能够反映您在屏幕上看到的内容,并使设计更具可扩展性;如果您将来需要添加数据的其他视图(摘要、最新等),则添加新视图,不需要部分视图,除非视图的主体变得更加复杂并且必须分解为部分视图。
URL 看起来像:
并对应于 ProductController 方法:
您可以使用如下路由启用路由:
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:
And correspond to ProductController methods:
You enable the routing with a route like:
Paul 的方法很好,但是如果您决定要传递 int,则需要创建一个视图模型。
在你的控制器中添加这个
然后将其传递到视图中
然后在视图中
Paul's method is good, but if you decide you want to pass the int, you need to create a view model.
In your controller add this
Then pass this into the view
And then in the view