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

查看:312
本文介绍了将多个结果集传递给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个产品在数据库中。我很好,在一个页面上显示它们,但我想使用(一个href =#类别)链接很像维基百科在页面顶部,我可以有类别列表,当你点击一个它带你到页面的适当部分。所以,我的观点在这种情况下会是这样:

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>

首先,我不完全确定这是正确的 m明确打开建议一个不同的方式做事情,但如果这是去的方式,那么我需要知道如何(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?

干杯!

推荐答案

您有两个选择。首先,您可以创建一个仅包含产品和类别集合的仅查看模型。第二,您可以在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 } );

这允许你使用强类型视图模型和intellisense,需要维护。

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天全站免登陆