【问题标题】:How can I create a List<ViewModel> with 2 other classes of models? MVC ASP.NET如何使用其他 2 类模型创建 List<ViewModel>? MVC ASP.NET
【发布时间】:2016-06-08 10:45:02
【问题描述】:

如何创建具有其他 2 类模型的 List

1 个模型:

public class Item
{
    public int ItemId { get; set; }
    public string Code { get; set; }
    public int QuantityInBase { get; set; }
    public bool IsHidden { get; set; }
}

2 型号:

public class JsonViewModel
    {
        public List<JsonItem> Items { get; set; }
    }

    public class JsonItem
    {
        public string Name { get; set; }
        public string Code { get; set; }
        public double Price { get; set; }
    }

正如您在上面看到的 - 两个类都有属性 Code - 它是连接器。

3 模型(目标 ViewModel)

public class ItemViewModel
    {
        public int ItemId { get; set; }
        public string Code { get; set; }
        public string Name { get; set; }
        public int QuantityInBase { get; set; }
        public decimal Price { get; set; }
        public bool IsHidden { get; set; }
    }

    public class ItemListViewModel
    {
        public List<ItemViewModel> ItemViewList;
    }

这就是我已经在控制器中尝试做的事情了。

      public ActionResult ItemsListPartial(JsonViewModel webSocketData)
    //webSocketData has values from webservice
            {
                ItemListViewModel model = new ItemListViewModel();
                var data = db.Items.ToList();

                foreach (var obj in data)
                {
                    model.ItemViewList = ...
                    // here, somehow i have to merge !var data! with                      
                    // !webSocketData! = and it has to be one 
                    // list of ItemListViewModel
                }

                return View("_ItemsListPartial", model);
            }

很难解释。我希望你知道我的意思。

【问题讨论】:

    标签: c# json asp.net-mvc mapping


    【解决方案1】:

    如果我理解正确,您可以在 code 属性上创建一个 linq join 查询。 比如:

    var rez= (from p in data
              join q in webSocketData.Items on p.Code equals q.Code
              select new ItemViewModel
              { 
                ItemId = p.ItemId,
                Code = p.Code,
                Name = q.Name,
                QuantityInBase = p.QuantityInBase ,
                Price = q.Price,
                IsHidden = p.IsHidden
              }).ToList();
    

    这将以leftOuterJoin 的方式返回ItemViewModel 的列表。

    【讨论】:

    • 我有一个错误:“join 子句中的表达式之一的类型不正确。调用 'Join' 时类型推断失败。”
    • 好的,我改为:“在 webSocketData.Items 中加入 q”我现在来测试。 brb
    • 好的工作正常!但是,如何在我的视图中显示此列表? “返回视图(“_ItemsListPartial”,rez);“。并在视图中添加:“@model List”但我仍然无法使用:“@Html.DisplayNameFor(model => model.Code)”
    • @DiPix 因为你有一个Model 的列表。所以你必须遍历所有的项目。类似:@(foreach var item in Model) Html.DisplayNameFor(item.Code)
    • @DiPix,如果你使用的是 Visual Studio,你可以让它创建你的局部视图。点击 addView 会出现一个模板,在那里选择一个模型(对你来说是ItemViewModel),检查 PartialView 并从那里的模板中选择 List。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-02
    • 1970-01-01
    • 1970-01-01
    • 2011-02-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多