在ASP.NET MVC控制器传递多个结果集到一个看法? [英] Passing multiple result sets to a view from a controller in ASP.NET MVC?

查看:102
本文介绍了在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());
        }
    }
}

管理控制器上的索引视图只是列出了系统中的产品,可以让我们点击一​​个产品来查看/编辑/删除它。真的很简单。然而,每个产品有一个类别ID,告诉我们它是在其被存储在一个单独的表,该表分类

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 =#类别)链接在页面的顶部就像维基百科这样做,我可以有类别列表,当你点击之一,它带给你的页面的相应部分。所以,我在这种情况下视图将看起来像这样:

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?

干杯!

推荐答案

您有两种选择。首先,你可以创建一个包含产品和类别的集合两者的仅查看模式。其次,你可以在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天全站免登陆