【发布时间】:2018-07-10 21:29:27
【问题描述】:
[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] 请帮我编写代码并将过滤后的列表返回给视图。 尝试了很多但没有工作..得到这个错误
传入字典的模型项的类型是'PagedList.PagedList'1[f__AnonymousType7`4[System.String,System.String,System.Int32,System.String]]',但是这个字典需要一个'PagedList.IPagedList'1[OpenOrderFramework.Models.Item]' 类型的模型项
请帮助我将匿名列表返回到我的 iagedlist 的代码
【问题讨论】:
标签: c# asp.net-mvc