将多个查询结果返回到各种视图部分

时间:2017-04-30 19:09:35

标签: c# asp.net-mvc database

我有两个div“最受欢迎”和“即将结束”。

我想在我的数据库中显示最受欢迎的项目,而拍卖日期的项目“即将结束”。

使用注释掉的代码,在Index()控制器中,在下面的代码中,我成功地能够在数据库中查询“Most Popluar”项目,并将这些结果返回到页面。但我试图将两组数据都返回到页面 - 最近流行的和即将结束的项目,部分视图 _AuctionTile 要使用?

我对即将结束的项目有查询。

下面的解决方案代表了我已经设定的解决方案路径,以实现能够返回两组数据 - 最受欢迎的项目和即将结束的视图。我想我有两个问题:

1)为什么我不能这样做:

var db = new AuctionsDataContext();
var mostPopular = db.Auctions.Where(x => x.EndTime > DateTime.Today).OrderByDescending(x => x.viewCount).ToArray();
var endingSoon = db.Auctions.Where(x => x.EndTime > DateTime.Today).OrderByDescending(x => x.EndTime).ToArray();
return View(mostPopular, endingSoon);

2)考虑到我在下面发布的解决方案路径,并在视图模型中创建两个列表结构,为什么Intellisense没有检测到属性mostPopularItems,如图所示屏幕截图?

@foreach (var item in Model.mostPopularItems)

视图

@model IEnumerable<MyAuctionApp.Models.Auction>

@{
    ViewBag.Title = "Home Page";
}

<div class="row">

    <div id="popular" class="col-md-4 col-lg-4">
        <h2>Most Popular Items</h2>

        @foreach (var item in Model.mostPopularItems)
        {
            @Html.Partial("_AuctionTile", item)

        }
</div>

<div id="ending" class="col-md-4 col-lg-4">
      <h2>Auctions Ending Soon</h2>
       @foreach (var item in Model)
        {
           @Html.Partial("_AuctionTile", item)
        }
 </div>

ViewModel

using MyAuctionApp.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace MyAuctionApp.ViewModels
{

public class AuctionViewModel
    {
    public static List<Auction> mostPopularItems;
    public static List<Auction> endingSoon;

        public AuctionViewModel(AuctionsDataContext db)
        {
        mostPopularItems = db.Auctions.Where(x => x.EndTime > 
DateTime.Today).OrderByDescending(x => x.viewCount).ToList();
        endingSoon = db.Auctions.Where(x => x.EndTime > 
DateTime.Today).OrderByDescending(x => x.EndTime).ToList();
        }
   }
}

控制器

using MyAuctionApp.Models;
using MyAuctionApp.ViewModels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace MyAuctionApp.Controllers
{
    [AllowAnonymous]
    public class HomeController : Controller

      {
        [AllowAnonymous]
        public ActionResult Index()
        {
        var db = new AuctionsDataContext();
       var dataModuleObject = new AuctionViewModel(db);
       return View(dataModuleObject);

      //var auctions = db.Auctions.ToArray();
     //var auctions = db.Auctions.Where(x => x.EndTime > 
     //DateTime.Today).OrderByDescending(x => x.viewCount);
     //return View(auctions);
         }
     }

正如您在附带的屏幕截图中看到的那样,mostPopularItemsAuctionViewModel中初始化的属性未被选中 由Intellisense作为现有的,在行

@foreach (var item in Model.mostPopularItems)

enter image description here

欢迎您的解决方案。但请,我是这个过程的新手。所以假设我需要你为我拼出一切。

1 个答案:

答案 0 :(得分:0)

您可以看到属性mostPopularItemsendingSoon,因为在您的视图模型中,您将其定义为static。在你看来你正在使用这一行:

@foreach (var item in Model.mostPopularItems)

这不起作用,因为Model包含AuctionViewModel的实例,而static只能通过类本身访问,而不能从实例访问。

您需要重构代码。视图模型必须如下所示:

public class AuctionViewModel
{
    public List<Auction> mostPopularItems {get; set; }
    public List<Auction> endingSoon {get; set; }

        public AuctionViewModel(AuctionsDataContext db)
        {
            mostPopularItems = db.Auctions.Where(x => x.EndTime > DateTime.Today).OrderByDescending(x => x.viewCount).ToList();
            endingSoon = db.Auctions.Where(x => x.EndTime > DateTime.Today).OrderByDescending(x => x.EndTime).ToList();
        }
   }
}

通过执行以下操作更改您的wiew模型类型:

@model MyAuctionApp.ViewModels.AuctionViewModel

因此,在重构之后,您的foreach代码将正常运行。