将匿名列表返回模型类型

时间:2018-07-10 11:59:26

标签: c# asp.net-mvc

[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]'类型的模型项

请使用代码帮助我返回匿名列表。我不知道该怎么办。请任何人向我展示示例代码以进行跟踪和更正,并向我展示它如何与模型及所有其他功能一起使用?

1 个答案:

答案 0 :(得分:0)

您的代码:

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

正在生成匿名类型的集合。创建具有这些属性的模型,然后将该模型作为PagedList传递到视图中。

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

现在在Linq查询中:

var ko= (from r in items.GroupBy(x => new { x.Catagorie, x.ItemName })
                              .Select(g => new MyObj()
                              {
                                  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<MyObj>

所以这里的结论是避免传递匿名类型并在PagedList中传递您的自定义模型。

希望这会对您有所帮助。

相关问题