简体   繁体   English

将ipagedList匿名类型转换为ipagedList模型

[英]convert ipagedList anonymous type to ipagedList Model

[1] i want to pass the anonymous list to the view but I'm finding it difficult please help me with the code: [1]我想将匿名列表传递给视图,但是我发现很难,请帮我提供代码:

i have the Model as : 我的模型为:

namespace OpenOrderFramework.Models
{
    [Bind(Exclude = "ID")]
    public class Item
    {
        private static NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger();
        [Key]
        [ScaffoldColumn(false)]
        public int ID { get; set; }

        [DisplayName("Catagorie:")]
        public int CatagorieId { get; set; }
        [Required(ErrorMessage = " Item name cannot be empty, this is required")]
        [DisplayName("Item Name")]
        public string ItemName { get; set; }
        [DisplayName("GRC TAG")]
        public string GRCTag { get; set; }
        [DisplayName("Location:")]
        public int LocationId { get; set; }
        [DisplayName("Item Name:")]
        public int itemnameId { get; set; }

        [Required(ErrorMessage = "Quantity of item in a Location is required!")]
        [DisplayName("Quantity in Store:")]
        public int ItemQty { get; set; }
        public DateTime DateCreated { get; set; }

controller is: 控制器是:

private ApplicationDbContext db = new ApplicationDbContext();
        // GET: Items
        public ActionResult Index(string sortOrder, string currentFilter, string searchString, int? page)
        {                      
            ViewBag.CurrentSort = sortOrder;
            ViewBag.NameSortParm = String.IsNullOrEmpty(sortOrder) ? "name_desc" : "Name";
            ViewBag.PriceSortParm = sortOrder == "Name" ? "Loc_desc" : "Location";
            IEnumerable<Item> items = db.Items;
           var ko= (from r in items.GroupBy(x => new { x.Catagorie, x.ItemName })
                                  .Select(g => new 
                                  {
                                      Catagorie = g.Key.Catagorie.Name,
                                      ItemName = g.Key.ItemName,
                                      ItemQty = g.Sum(s => s.ItemQty),
                                      Location = g.First().Location.Name

                                  })
                         select r).ToList();


                if (!string.IsNullOrWhiteSpace(searchString))
                {
                    items = items.Where(s => s.ItemName.Contains(searchString.ToUpper())
                                               || s.Catagorie.Name.ToUpper().Contains(searchString.ToUpper()) ||
                                               s.Location.Name.ToUpper().Contains(searchString.ToUpper())).ToList().ToPagedList(page ?? 1, 20);
                    //}

                }
                else
                {
                    items = items.ToList().ToPagedList(page ?? 1, 10);
                }

            return View(ko.ToList().ToPagedList(page ?? 1, 20));

        }

and the View is : 并且视图是:

@model PagedList.IPagedList<OpenOrderFramework.Models.Item>
@using PagedList.Mvc;
@using PagedList;

<table class="table">
    <tr>
        <th>
            Catagory
        </th>
       <th>
            Item Name.
        </th>
 <th>
            Quantity.
        </th>

 @foreach (var item in Model)
    {
        <tr>
            <td>
                <font color="RED">
                @Html.DisplayFor(modelItem => item.Catagorie.Name)
                </font>
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.ItemName)
            </td>            
            <td>
                @Html.DisplayFor(modelItem => item.ItemQty)
            </td>

[2] please help me with the code and return the filterd list to the view . [2]请使用代码帮助我,并将过滤后的列表返回到视图。 tried so many but not working.. got this error 尝试了很多,但不起作用..得到了这个错误

The model item passed into the dictionary is of type 'PagedList.PagedList 1[<>f__AnonymousType7 4[System.String,System.String,System.Int32,System.String]]', but this dictionary requires a model item of type 'PagedList.IPagedList`1[OpenOrderFramework.Models.Item]' 传递到字典中的模型项的类型为'PagedList.PagedList 1[<>f__AnonymousType7 4 [System.String,System.String,System.Int32,System.String]]',但是此字典需要类型为'的模型项PagedList.IPagedList`1 [OpenOrderFramework.Models.Item]'

please help me with the code to return the anonymous list to my ipagedlist 请使用代码帮助我,以将匿名列表返回到我的ipagedlist

I'm not sure about your OpenOrderFramework.Models.Item class but as can infer from code you need to add "new Item": 我不确定您的OpenOrderFramework.Models.Item类,但是可以从代码中推断出您需要添加“新项目”:

var ko= (from r in items.GroupBy(x => new { x.Catagorie, x.ItemName })
                              .Select(g => new new Item
                              {
                                  Catagorie = g.Key.Catagorie.Name,
                                  ItemName = g.Key.ItemName,
                                  ItemQty = g.Sum(s => s.ItemQty),
                                  Location = g.First().Location.Name

                              })
                     select r).ToList();

in thew view you are defining the expected model 在thew视图中,您正在定义期望的模型

@model PagedList.IPagedList<OpenOrderFramework.Models.Item>

So, you are able to pass a anonymous type to the view, BUT, you won't be able to have a strongly typed class in the view since it is anonymous. 因此,您可以将匿名类型传递给视图,但是,由于它是匿名的,因此您将无法在视图中具有强类型的类。 You cannot have the best of both worlds (thats the point of being anonymous) 您不能两全其美(这就是匿名的意义)

Solution 1. You want strongly typed and intelli-sense 解决方案1.您需要强类型输入和智能感知

Declare a class and forget about anonymous, don't be shy ;) 宣布一个课程,忘记匿名,不要害羞;)

public class Item
{
   public string Catagorie{get; set;}
   public string ItemName{get; set;}
   public int ItemQty{get; set;}
   public string Location{get; set;}
}  

Query will be something similar to this: 查询将类似于以下内容:

var ko= (from r in items.GroupBy(x => new { x.Catagorie, x.ItemName })
                              .Select(g =>  
                                         new Item()
                                  {
                                     Catagorie = g.Key.Catagorie.Name,
                                     ItemName = g.Key.ItemName,
                                     ItemQty = g.Sum(s => s.ItemQty),
                                     Location = g.First().Location.Name})
                     select r).ToList();

Last, change the model to be your recently declared class: @model PagedList.IPagedList<YOURNS.Item> 最后,将模型更改为您最近声明的类: @model PagedList.IPagedList<YOURNS.Item>

Solution 2 解决方案2

Remove the @model declaration in the view and write the names of the variables without using strongly typed help. 删除视图中的@model声明,并在不使用强类型帮助的情况下编写变量名称。 Just the name. 只是名字。

@foreach (var item in Model)
    {
        <tr>
            <td>
                <font color="RED">
                @Html.Display("Catagorie")
                </font>
            </td>
            <td>
                @Html.Display("ItemName")
            </td>            
            <td>
                @Html.Display("ItemQty")
            </td>

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

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