在将此匿名返回到视图时遇到错误 [英] having Error returning this anonymous to the view
问题描述
[1]我想将匿名列表传递给视图,但是我发现很难请使用以下代码帮助我:
[1] i want to pass the anonymous list to the view but I'm finding it difficult please help me with the code:
我的模型为:
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.尝试了很多但是没有用..得到了这个错误
[2] please help me with the code and return the filterd list to the view . tried so many but not working.. got this error
传递到字典中的模型项的类型为'PagedList.PagedList'1 [<> f__AnonymousType7`4 [System.String,System.String,System.Int32,System.String]]',但是此字典需要'PagedList.IPagedList'1 [OpenOrderFramework.Models.Item]'类型的模型项
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]'
请使用代码帮助我,以将匿名列表返回到我的ipagedlist
please help me with the code to return the anonymous list to my ipagedlist
推荐答案
第一行中的视图状态...
your view states in the very first line ...
@model PagedList.IPagedList<OpenOrderFramework.Models.Item>
您应该将其作为View()调用的参数传递给的对象必须具有这种类型...
the object you are supposed to hand over as a parameter to the View() call needs to have this type ...
不支持在剃刀视图中将匿名对象作为模型,您可以将其视为动态对象,但是会松散编译时间检查(不应该这样做)
having anonymous objects as model in a razor view is unsupported, you can treat them as dynamics but you loose compile time checking (you shouldn't do that)
代替匿名对象,为您的视图创建模型或视图模型对象并将其移交给他们,也许您可以为此重用item类
instead of an anonymous object, create model or viewmodel objects for your view and hand them over, maybe you can reuse the item class for this
您想要一个代码示例...
you want a code sample...
问题从您的匿名类型开始...
the problem starts here with your anon type ...
.Select(g => new
{
Catagorie = g.Key.Catagorie.Name,
ItemName = g.Key.ItemName,
ItemQty = g.Sum(s => s.ItemQty),
Location = g.First().Location.Name
})
使用适合您当前命名约定和应用程序体系结构的名称创建一个新类:该类将保存不代表数据库中的实体而是分组聚合结果的数据.如果该数据仅在一个视图中使用,则有资格作为视图模型...当不确定此类属于应用程序的哪一层时,请将其视为数据传输对象或DTO
create a new class with a name suitable for your current naming convention and application architecture: the class will hold data that does not represent an entity in your db but a grouped aggregation result. if that data is only to be used in that one view, it qualifies as a view model ... when in doubt what layer in your application this class belongs to, see it as a data transfer object or DTO
在此示例中,我将其称为ItemSumDTO,它具有与您的匿名类型相同的属性
in this example i will call it ItemSumDTO, it has the same properties as your anon type
创建ItemSumDTO而不是在上面的选择中创建匿名类型对象
instead of creating anon type objects in the select above, create ItemSumDTOs
.Select(g => new
成为
.Select(g => new ItemSumDTO
如果您的ORM替换存在问题,则此时应坚持使用anon对象,稍后,在对.ToList()的调用之后,引入另一个 .Select(x => new ItemSumDTO {Catagorie =x.Catagorie,ItemName = x.ItemName,ItemQty = x.ItemQty,位置= x.Location})
if your ORM has a problem with that replacement, stick to the anon object at this time, and later, after the call to .ToList() introduce another .Select(x=> new ItemSumDTO{Catagorie = x.Catagorie, ItemName = x.ItemName, ItemQty = x.ItemQty, Location = x.Location})
在您的视图中,将 @model
定义更改为 PagedList.IPagedList< ItemSumDTO>
in your View, change the @model
definition to be a PagedList.IPagedList<ItemSumDTO>
这篇关于在将此匿名返回到视图时遇到错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!