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

时间:2018-07-09 15:11:19

标签: c# model-view-controller

[1]我想将匿名列表传递给视图,但是我发现很难 请使用以下代码帮助我:

我的模型为:

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; }

控制器为:

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));

        }

视图为:

@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]请帮助我提供代码,并将过滤后的列表返回到view。 尝试了很多但是没有用..得到了这个错误

传递到字典中的模型项的类型为'PagedList.PagedList 1[<>f__AnonymousType7 4 [System.String,System.String,System.Int32,System.String]]',但是此字典需要模型项类型为'PagedList.IPagedList`1 [OpenOrderFramework.Models.Item]'

请使用代码帮助我,以将匿名列表返回到我的ipagedlist

2 个答案:

答案 0 :(得分:0)

我不确定您的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();

答案 1 :(得分:0)

在thew视图中,您正在定义期望的模型

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

因此,您可以将匿名类型传递给视图,但是,由于它是匿名的,因此无法在视图中具有强类型的类。您不能两全其美(那就是匿名)

  

解决方案1.您要强力输入并具有智能感

声明一个类,忘了匿名,不要害羞;)

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

查询将类似于以下内容:

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();

最后,将模型更改为您最近声明的类: @model PagedList.IPagedList<YOURNS.Item>

  

解决方案2

在视图中删除@model声明,并在不使用强类型帮助的情况下编写变量的名称。只是名字。

@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>