是否有可能在显示的模型/ viewmodel上包含一个不包含导航属性的模型

时间:2018-07-18 08:44:54

标签: c# asp.net-mvc entity-framework linq-to-entities

我有2个为以下对象创建视图模型的类:

ELSE 0

ViewModel:

NULL

我正在尝试显示所有糖果的列表,包括当前购物车中每种糖果的数量(该糖果在当前购物车中可能不存在)。

我已经编写了所需数据的基本查询,但是不确定如何以MVC方式执行此操作:

ELSE

控制器:

SUM()

查看:

public class Sweet
{
    public int SweetID { get; set; }

    public int CategoryID { get; set; }
    public Category Category { get; set; }
    public string SweetName { get; set; }
    public bool Singular { get; set; }
    public string Description { get; set; }
    public decimal Price { get; set; }


    public virtual ICollection<Cart> Carts { get; set; }
}


public class Cart
{
    [Key]
    public int RecordID { get; set; }
    public string CartID { get; set; }
    public int SweetID { get; set; }
    public int PemixID { get; set; }
    public int Qty { get; set; }
    public System.DateTime DateCreated { get; set; }

    public Sweet Sweet { get; set; }
    public PreMix PreMix { get; set; }

}

1 个答案:

答案 0 :(得分:1)

通过查看视图,您想显示“ SweetName”和“ Qty”。

因此,您必须像这样重构“ SweetViewModel”

public class SweetViewModel
    {
        //From Sweet Model
        public int SweetID { get; set; }
        public int CategoryID { get; set; }
        public Category Category { get; set; }
        public string SweetName { get; set; }
        public bool Singular { get; set; }
        public string Description { get; set; }
        public decimal Price { get; set; }

        //From Cart Model
        public int Qty { get; set; }
    }

根据您的输出,您必须像这样设计视图

@model IEnumerable<WebApplication1.ViewModels.SweetViewModel>

@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>SweetTestArea</title>
</head>
<body>
    <p>
        @Html.ActionLink("Create New", "Create")
    </p>
    <table class="table">    
    @foreach (var item in Model) {
        <tr>
            <td>
                @Html.DisplayFor(modelItem => item.SweetID)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.CategoryID)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.SweetName)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Description)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Price)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Qty)
            </td>
        </tr>
    }

    </table>
</body>
</html>

您的控制器代码将类似于

 public ActionResult SweetTestArea()
        {
            List<SweetViewModel> viewModel = new List<SweetViewModel>();

            var q = (from s in db.Sweets
                     join c in db.Carts on s.SweetID equals c.SweetID
                     where c.CartID == "7125794e-38f4- 4ec3-b016-cd8393346669"
                     select new SweetViewModel
                     {
                         CategoryID = s.CategoryID,
                         Description = s.Description,
                         Price = s.Price,
                         Qty = c.Qty,
                         SweetID = s.SweetID,
                         SweetName = s.SweetName
                     });

            viewModel = q.ToList();

            return View(viewModel);
        }

如果您想退出联接,则您的linq查询将为

var q = (from s in db.Sweets
                     join c in db.Carts on s.SweetID equals c.SweetID into j
                     from c in j.DefaultIfEmpty()
                     where c.CartID == "7125794e-38f4- 4ec3-b016-cd8393346669"
                     select new SweetViewModel
                     {
                         CategoryID = s.CategoryID,
                         Description = s.Description,
                         Price = s.Price,
                         Qty = c.Qty,
                         SweetID = s.SweetID,
                         SweetName = s.SweetName
                     });

我建议您不要使用左联接,因为您希望从Sweet和Cart的两个模型中都在购物车中显示数据,这意味着购物车中有至少1个数量的商品。

编辑:

根据我对您的最后一条评论的理解,您希望每件商品都显示在购物车中,无论是否显示在购物车模型中

为此

1)让我们先拿购物车中的物品

List<SweetViewModel> cart = new List<SweetViewModel>();

            var q = (from s in db.Sweets
                     join c in db.Carts on s.SweetID equals c.SweetID into l
                     from c in l.DefaultIfEmpty()
                     where c.CartID == "7125794e-38f4- 4ec3-b016-cd8393346669"
                     select new SweetViewModel
                     {
                         CategoryID = s.CategoryID,
                         Description = s.Description,
                         Price = s.Price,
                         Qty = c.Qty,
                         SweetID = s.SweetID,
                         SweetName = s.SweetName
                     });


            cart = q.ToList();

2)然后获取所有糖果并将其投影到SweetViewModel

List<SweetViewModel> allSweets = new List<SweetViewModel>();

            var w = (from s in db.Sweets
                     select new SweetViewModel
                     {
                         CategoryID = s.CategoryID,
                         Description = s.Description,
                         Price = s.Price,
                         Qty = 0,
                         SweetID = s.SweetID,
                         SweetName = s.SweetName
                     });

            allSweets = w.ToList();

3)过滤掉购物车中存在的那些糖果    您将在下面的过滤列表中得到甜蜜的ID

var filteredSweets = allSweets.Select(x => x.SweetID).Where(x => !cart.Select(y => y.SweetID).Contains(x)).ToList();

4)将经过过滤的糖果添加到模型中

List<SweetViewModel> viewModel = new List<SweetViewModel>();

            foreach (var i in filteredSweets)
                viewModel.Add(allSweets.Where(x => x.SweetID == i).Take(1).SingleOrDefault());

5)然后将您的购物车商品添加到视图模型中

viewModel.AddRange(cart);

并将此视图模型返回视图。

您可以根据需要使用order by对购物车中的物品进行排序。