简体   繁体   English

MVC,查看显示数据

[英]MVC, view displaying data

Hey, I am new to asp and I would like to ask you for some help. 嘿,我是ASP的新手,我想向您寻求帮助。 I built store with MvcMusicStore tutorial help. 我使用MvcMusicStore教程帮助建立了商店。 Now I want to add a View to manage orders which will display just OrderID (in Index View), then more info in Details View. 现在,我想添加一个视图来管理订单,该视图将仅显示OrderID(在“索引视图”中),然后在“详细信息视图”中显示更多信息。 Info will come from table which looks like this: 信息将来自如下表格:

When I list OrderID, its multiplying because each product creates new record with the same OrderID in the table. 当我列出OrderID时,它会成倍增加,因为每个产品都会在表中创建具有相同OrderID的新记录。 Is there any way to display each Id just once? 有没有办法只显示一次每个ID?

Then I tried to display more info in Detaild View but I failed again. 然后,我尝试在详细视图中显示更多信息,但再次失败。 I used this code in Controller: 我在控制器中使用了以下代码:

        public ActionResult Details(int id)
    {
        var orderdetail = storeDB.OrderDetails.Single(a => a.Order.OrderId == id);
        return View(orderdetail);
    }

but obviously it wont work because only one element can be displayed. 但很明显,它将无法正常工作,因为只能显示一个元素。 I also tried with foreach loop in Details.aspx but I was getting some IEnumerables-related error. 我也尝试了Details.aspx中的foreach循环,但遇到了IEnumerables相关的错误。 Any advice is welcome, sorry for newbie question and bad English. 欢迎任何建议,对于新手问题和英语不好,我们深表歉意。 Thank you. 谢谢。

Edit: Here is Controller's code for Index View (Product in my table equals Album in tutorial): 编辑:这是控制器的索引视图代码(我的表中的产品等于教程中的相册):

        public ActionResult Index()
    {
        var manageorders = storeDB.OrderDetails
        .Include("Product").Include("Order")
        .ToList();
        return View(manageorders);
    }

And Details View code: 和详细信息查看代码:

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/MasterPage.Master" Inherits="System.Web.Mvc.ViewPage<ss.Models.OrderDetail>" %>

Index 指数

<div id="style3">manage orders</div>
<div id="style1">
<table>
    <tr>
        <th></th>
        <th>
        user
        </th>
        <th>
        data
        </th>
        <th>
        product
        </th>
        <th>
        quantity
        </th>

    </tr>

<% foreach (var item in Model) { %>

    <tr>
        <td>
            <%: Html.ActionLink("Edit", "Edit", new { id=item.Order.OrderId }) %> |
            <%: Html.ActionLink("Details", "Delete", new { id=item.Order.OrderId })%>
        </td>
        <td>
            <%: item.Order.Username %>
        </td>
                    <td>
            <%: item.Order.OrderDate %>
        </td>
        <td>
            <%: item.Quantity %>
        </td>
                    <td>
            <%: item.Quantity %>
        </td>
    </tr>

<% } %>

<p>
    <%: Html.ActionLink("Create New", "Create") %>
</p>

And error I am recieving: 我收到的错误是:

Compiler Error Message: CS1579: foreach statement cannot operate on variables of type 'ss.Models.OrderDetail' because 'ss.Models.OrderDetail' does not contain a public definition for 'GetEnumerator' 编译器错误消息:CS1579:foreach语句无法对类型为“ ss.Models.OrderDetail”的变量进行操作,因为“ ss.Models.OrderDetail”不包含“ GetEnumerator”的公共定义

Single() will throw an exception if there's more than 1 element in the sequence, so the error here is because each Order can have many OrderDetails (this is your IEnumerable-ish error). 如果序列中有1个以上的元素, Single()将引发异常,所以这里的错误是因为每个Order可以具有许多OrderDetails(这是IEnumerable-ish错误)。 If you wanted to select the first match, when there could be many, use the First() method, which won't throw if there's more than one. 如果要选择第一个匹配项,则当可能有多个匹配项时,请使用First()方法;如果有多个匹配项,则不会抛出该匹配项。

It depends really what your controller is working with - Orders or OrderDetails. 这实际上取决于您的控制器正在使用什么-Orders或OrderDetails。 This would be the difference between Orders/Details/1 and OrderDetails/Details/1 , where the first would be focussing on the whole order and all of its details, and the second would be looking at one specific line. 这将是Orders/Details/1OrderDetails/Details/1之间的区别,其中第一个将关注整个订单及其所有详细信息,第二个将关注特定的一行。

If you wanted to work with the order - which it looks like you're going for - then you'd need a reference of some kind from the Order to its collection of OrderDetails. 如果您想使用该订单(看起来像您要使用的订单),则需要从Order到其OrderDetails集合的某种类型的引用。 If you're using something like Entity Framework like in the MusicStore tutorial, then you'll get this collection as part of your entity model. 如果您在MusicStore教程中使用的是诸如实体框架之类的东西,那么您将获得此集合作为实体模型的一部分。 You would pass the Order to the view, and could then access the OrderDetails collection in a for each or similar loop eg 您可以将Order传递到视图,然后可以在for每个或类似循环中访问OrderDetails集合,例如

public ActionResult details(int id)
{
   Order viewModel = storeDb.Orders.Single(o => o.Id = id);
   return View(viewModel);
}

This wouldn't throw if Id is the unique per order, because there is only a single match (not many). 如果Id是每个订单的唯一身份,则不会抛出该错误,因为只有一个匹配项(不多)。 Your view could then have something like: 您的视图可能会像这样:

<ul>
<% foreach (OrderDetails od in Model.OrderDetails) { %>
   <li><%: od.UnitPrice %> (Or whatever information you wanted to show) </li>
<% } %>
</ul>

I'm assuming you would be using a strongly typed view of type Order, otherwise you'd have to work with ViewData, but it looks from your question like you're happy enough with those concepts already. 我假设您将使用Order类型的强类型视图,否则就不得不使用ViewData,但是从您的问题来看,您似乎已经对这些概念感到满意。

Edit - adding more... 编辑-添加更多...

1.The typing of your page is causing you some problems, make sure you are clear in your own mind exactly what your Model is. 1.键入页面会给您带来一些问题,请确保您自己清楚自己的模型是什么。 If you are passing in a list - like you would in an Index action - you need a page that inherits ViewPage> (you can see this in the <%@ Page %> declaration at the top of the page). 如果要传递列表(就像在Index操作中一样),则需要一个继承ViewPage>的页面(您可以在页面顶部的<%@ Page%>声明中看到它)。 If you're going to write code like "for each... in model", that would imply that your model must contain more than one thing. 如果要编写“针对模型中的每个……”这样的代码,则意味着您的模型必须包含多个要素。 This is where your "GetEnumerator" error message is from. 这是“ GetEnumerator”错误消息的来源。

Your view as posted inherits ViewPage, ie the model is a single object, not a collection of objects, and so "for each ... in model" has no sensible meaning. 您发布的视图继承了ViewPage,即模型是单个对象,而不是对象的集合,因此“对于模型中的每个……”没有任何意义。 The type of your View should be the same as the type being passed down by your controller. 视图的类型应与控制器传递的类型相同。 Your Index method passes a List, and List is an example of an IEnumerable. 您的Index方法传递一个List,而List是IEnumerable的一个示例。 Your details however passes a single YourModelClass. 但是,您的详细信息会传递单个YourModelClass。

2.The error message "Compiler Error Message: CS1061: 'ss.Models.OrderDetail' does not contain a definition for 'OrderDetails'" - check your model, if there are relationships (foreign keys etc) between your tables, they should be joined on the model diagram, and you'd normally have navigation properties as well. 2.错误消息“ Compiler Error Message:CS1061:'ss.Models.OrderDetail'不包含'OrderDetails'的定义”-检查模型,如果表之间存在关系(外键等),则应联接到模型图上,通常也具有导航属性。 The fact that you're getting this message means that something in that isn't there. 您收到此消息的事实意味着其中不存在某些内容。

3.I'd suggest it might be worth checking out some of the videos, as the explanations are quite handy. 3.我建议您看一些视频可能是值得的,因为解释非常方便。 Try some of the videos at http://www.asp.net/mvc/fundamentals . http://www.asp.net/mvc/fundamentals上尝试一些视频。 I Joe Stagner's "For the rest of us" series are very good, and as a starting point Stephen Walther's "Creating a Movie DB Application" would help, as it shows this kind of thing. 乔·斯塔格纳(Joe Stagner)的“对我们其余的人”系列非常出色,而斯蒂芬·沃尔瑟(Stephen Walther)的“创建电影数据库应用程序”将作为起点,因为它说明了这种事情。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM