将多个结果集从 ASP.NET MVC 中的控制器传递给视图? [英] Passing multiple result sets to a view from a controller in ASP.NET MVC?

查看:16
本文介绍了将多个结果集从 ASP.NET MVC 中的控制器传递给视图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有一个控制器设置如下:

So I have a controller set up as follows:

using NonStockSystem.Models;

namespace NonStockSystem.Controllers
{
    [Authorize(Users = "DOMAIN\rburke")]
    public class AdminController : Controller
    {    
        private NonStockSystemDataContext db = new NonStockSystemDataContext();

        public ActionResult Index()
        {
            var enumProducts = from p in db.Products select p;
            ViewData["Title"] = "Administration";
            return View(enumProducts.ToList());
        }
    }
}

管理控制器上的索引视图仅列出系统中的产品,并允许我们单击产品以查看/编辑/删除它.真的很简单.但是,每个产品都有一个 CategoryID,它告诉我们它属于哪个类别,存储在单独的表中.

The Index view on the Admin controller just lists the products in the system and allows us to click on a product to view / edit / delete it. Really simple. However each product has a CategoryID which tell us which Category it is in which is stored in a separate table.

(非常简化的)当前视图是这样的:

The (very simplified) current view is this:

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" AutoEventWireup="true" CodeBehind="Index.aspx.cs" Inherits="NonStockSystem.Views.Home.Admin" %>
<%@ Import Namespace="NonStockSystem.Models" %>
<asp:Content ID="Content1" ContentPlaceHolderID="MainContent" runat="server">

<%
foreach (Product p in (IEnumerable)ViewData.Model)
{ %>

    <%=p.Name.ToString() %> (<a href="/Admin/Edit/<%=p.ID.ToString() %>">Edit</a> - <a href="/Admin/Delete/<%=p.ID.ToString() %>">Delete</a>)<br />

<%
} %>   

</asp:Content>

目前这很好,因为在我开发和测试时系统中只有 10 或 15 个产品,但是一旦我部署它,就会有大约.数据库中有 300 种产品.我可以将它们全部显示在一个页面上,但是我想使用 (a href="#category") 链接,就像维基百科在页面顶部所做的那样,我可以获得类别列表,当您单击时一个它会将您带到页面的相应部分.因此,在这种情况下,我的观点将如下所示:

This is fine at the moment as there are only 10 or 15 products in the system whilst I develop and test it however once I deploy it there will be approx. 300 products in the database. I'm fine with displaying them all on one page however I'd like to use (a href="#category") links much like Wikipedia does so at the top of the page I can have the list of categories and when you click one it brings you to the appropriate section of the page. So, my view in that case will look like so:

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" AutoEventWireup="true" CodeBehind="Index.aspx.cs" Inherits="NonStockSystem.Views.Home.Admin" %>
<%@ Import Namespace="NonStockSystem.Models" %>
<asp:Content ID="Content1" ContentPlaceHolderID="MainContent" runat="server">

<ul>
<%
foreach (Category c in (IEnumerable)ViewData.Model)
{ %>
    <li><a href="#<%=c.Name.ToString() %>"><%=c.Name.ToString() %></a></li>
<%
} %>
</ul>

<hr />

<%
foreach (Category c in (IEnumerable)ViewData.Model)
{ %>
    <% // Display the category name above all products from that category %>
    <h2><a name="<%=c.Name.ToString() %>"><%=c.Name.ToString() %></a></h2>

    <% // Need to limit the following foreach to grab only products in this category
    foreach (Product p in (IEnumerable)ViewData.Model)
    { %>
        <%=p.Name.ToString() %> (<a href="/Admin/Edit/<%=p.ID.ToString() %>">Edit</a> - <a href="/Admin/Delete/<%=p.ID.ToString() %>">Delete</a>)<br />
    <%
    } %>
} %>

</asp:Content>

首先,我不完全确定这是做到这一点的正确"方式,所以我绝对愿意接受不同的做事方式的建议,但如果这是要走的路,那么我需要知道如何(1) 将两个结果集传递给视图(产品和类别)和 (2) 循环遍历每个 foreach 循环中的产品子集,只抓取适当类别中的产品?

Firstly, I'm not entirely sure this is the "right" way to do this so I'm definitely open to suggestions of a different way of doing things but if this is the way to go then I need to know how to (1) pass two result sets to the view (Products and Categories) and (2) loop through a subset of the Products in each foreach loop grabbing only the ones in the appropriate category?

干杯!

推荐答案

您有两种选择.首先,您可以创建一个包含 Products 和 Categories 集合的仅查看模型.其次,您可以在 ViewData 中传递一个或两个模型,而不是将其设置为页面的模型.

You have two alternatives. First, you can create a view-only model that consists of both the Products and Categories collections. Second, you can have one or both of the models be passed in ViewData instead of set as the Model for the page.

public class CategoryProduct
{
    public IEnumerable<Product> Products { get; set; }
    public IEnumerable<Category> Categories {get; set; }
}

....

return View( new CategoryProduct { Products = products,
                                   Categories = categories } );

这允许您使用强类型视图模型并获得智能感知,但需要额外维护一个类.

This allows you to use the strongly typed view model and get intellisense, but costs an extra class that you need to maintain.

或(一种选择)

ViewData["categories"] = categories;
return View( products );

这需要为类别转换 ViewData,以便您可以使用强类型.在这种情况下的产品是模型.您可以交换这些或将两者都放在 ViewData 中.

This requires casting the ViewData for Categories so you can use it strongly typed. Products in this case is the model. You could swap these or put both in ViewData.

就你的方法而言,我没问题.一种替代方法是将其分页,以便并非所有类别都存在于同一页面上,以减少页面长度.您仍然会在顶部列出所有类别,但有些类别可能需要回发才能获得新的数据页面.这可能有点复杂,但是当产品数量增加时会加快加载时间.另一种方法是使用向下钻取.首先选择一个类别,然后只显示该类别中的产品.这些也可以分页.我认为这是亚马逊、Buy.com 等网站更典型的做法.它们还允许额外的过滤(类别只是顶级).

As far as your approach, I'm okay with it. One alternative would be to make it paged so that not all categories exist on the same page to keep the page length down. You'd still have all the categories listed at the top, but some might require a postback instead to get a new page of data. This is probably a little complicated but will speed up load time when the number of products grows. Another way to do it is using a drilldown. First choose a category, then only products in that category display. These could also be paged. I think this is more typically the way sites like Amazon, Buy.com, etc. do it. They also allow additional filtering (category being only the top level).

这篇关于将多个结果集从 ASP.NET MVC 中的控制器传递给视图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆