ASP.Net MVC 将多个参数传递给视图

发布于 2024-08-20 09:45:02 字数 1163 浏览 3 评论 0原文

在 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>&nbsp;<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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

不即不离 2024-08-27 09:45:02

您的视图应该类似于:

<%@ Page Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<Models.MyModel>" %>

然后在 MyModel 中

公开属性:

public bool RenderDetailView {get;set;}

在您的控制器操作中:

public ActionResult Products(string id, int? renderview)
{
    var products = productRepository.GetProducts(id).ToList();
    return View("Products", new MyModel {RenderDetailView = renderview.HasValue});
}

然后在您的视图中,进行如下检查:

<% if (Model.RenderDetailView)

理想情况下,视图展示自身所需的所有属性、参数或数据都应该是模型的一部分。

我希望它有帮助。

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.

又怨 2024-08-27 09:45:02

另一种方法是使用 Restful URL 来调用适当的控制器操作和视图。

这使得 url 能够反映您在屏幕上看到的内容,并使设计更具可扩展性;如果您将来需要添加数据的其他视图(摘要、最新等),则添加新视图,不需要部分视图,除非视图的主体变得更加复杂并且必须分解为部分视图。

URL 看起来像:

~/product/1/detail

~/product/1/thumbnail

并对应于 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);
}

您可以使用如下路由启用路由:

{controller}/{id}/{action}

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}
伤痕我心 2024-08-27 09:45:02

Paul 的方法很好,但是如果您决定要传递 int,则需要创建一个视图模型。

在你的控制器中添加这个

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

然后将其传递到视图中

return View(new ProductsFormViewModel(products, renderview);

然后在视图中

Inherits="System.Web.Mvc.ViewPage<YourNamespace.Controllers.ProductsFormViewModel>"

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

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>"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文