asp.net-mvc ASP.Net MVC 将多个参数传递给视图
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2199440/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
ASP.Net MVC Passing multiple parameters to a view
提问by Nicholas Murray
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>
采纳答案by Nitin Midha
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.
我希望它有帮助。
回答by Paul
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.
这使 url 反映您在屏幕上看到的内容,并使设计更具可扩展性;如果您将来需要添加数据的其他视图(摘要、最新等),您可以添加新视图,不需要局部视图,除非视图主体变得更加复杂并且必须分解为局部视图.
The URLs would look like:
URL 将如下所示:
~/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}
回答by Wil
Paul's method is good, but if you decide you want to pass the int, you need to create a view model.
保罗的方法不错,但是如果你决定要传递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>"

