就在我面前回答? (Generic.List 需要 Generic.IEnumerable 错误类型的模型项)
Answer Right in Front of Me? (Generic.List requires a model item of type Generic.IEnumerable Error)
好的,我已经研究了好几天了,但我似乎并没有取得太大进展。我敢肯定,人们已经厌倦了看到与此类错误相关的问题,因为这里有很多负载,而且我已经经历了很多负载,但是我似乎无法将所有答案拼凑在一起以获得适合我的场景的完整解决方案。
我将从发布我的代码开始...
控制器 - HomeController.cs(儿童操作)
[ChildActionOnly]
public ActionResult Widget()
{
var eventId = ViewEventPropertyService.Get().Select(y => new { y.EventId }).ToList();
return PartialView("Widget", eventId);
}
我可以通过使用调试器看到上面检索到的结果是我期望的
型号 - Event.cs
public partial class Event : Auditable
{
#region Primitive Properties
public Event()
{
this.Parents = new List<EventRelationship>();
this.Children = new List<EventRelationship>();
this.Properties = new List<EventProperty>();
this.ViewProperties = new List<ViewEventProperty>();
}
public virtual int Id { get; set; }
}
我不确定以上内容与问题的相关性如何,但我想我会把它包含在 cse
ViewModel - EventDetailsViewModel.cs
public class EventDetailsViewModel : IEnumerable<Solution.Domain.Event>
{
public IEnumerable EventId { get; set; }
public IEnumerator<Solution.Domain.Event> GetEnumerator()
{
return GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
}
我根据另一个问题的答案将 GetEnumertor 部分放入,但我不确定它是否正确。
查看 - index.cshtml
@Html.Action("Widget")
我只是在视图中调用上面的内容,我认为不需要任何其他信息(如果需要请告诉我)。
部分视图 - Widget.cshtml
@model IEnumerable<Solution.Web.Models.EventDetailsViewModel>
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title></title>
</head>
<body>
@foreach (var item in Model)
{
<div>@item.EventId</div>
}
HTML.Display
</body>
</html>
我认为上面列出的结果是正确的,但我不是 100%
我的目标
好的,所以我要做的只是列出来自数据库的 EventId,这些 EventId 由 Widget() 子操作在视图中获取。
我的问题
使用我当前的代码,我收到以下错误消息。
The model item passed into the dictionary is of type
'System.Collections.Generic.List1[<>f__AnonymousType22
1[System.Int32]]',
but this dictionary requires a model item of type
'System.Collections.Generic.IEnumerable`1[Solution.Web.Models.EventDetailsViewModel]'.
我可以看到错误是说该操作是 returning Generic.List 而我要求的是 IEnumerable 但我不知道如何 return 数据以使其成为这种类型。
所以,有人能帮我解决这个问题吗,因为我现在一事无成...?
这个问题现在已经够长了,所以我会把它留在那里,但如果有人需要更多细节,那就问吧。
谢谢
J
我不明白你为什么要在 EventDetailsViewModel
中实施 IEnumerable<Solution.Domain.Event>
,也许我在这里错过了大局。为了解决模型类型不正确的具体问题,您可以进行这样的更改:
首先,简化视图模型(并将 Id 类型更改为 int):
public class EventDetailsViewModel
{
public int EventId { get; set; }
}
其次 - 在您的控制器方法中使用此类型而不是匿名对象:
public ActionResult Widget()
{
var eventIds = ViewEventPropertyService.Get()
.Select(y => new EventDetailsViewModel { EventId = y.EventId })
.ToList();
return PartialView("Widget", eventIds);
}
好的,我已经研究了好几天了,但我似乎并没有取得太大进展。我敢肯定,人们已经厌倦了看到与此类错误相关的问题,因为这里有很多负载,而且我已经经历了很多负载,但是我似乎无法将所有答案拼凑在一起以获得适合我的场景的完整解决方案。
我将从发布我的代码开始...
控制器 - HomeController.cs(儿童操作)
[ChildActionOnly]
public ActionResult Widget()
{
var eventId = ViewEventPropertyService.Get().Select(y => new { y.EventId }).ToList();
return PartialView("Widget", eventId);
}
我可以通过使用调试器看到上面检索到的结果是我期望的
型号 - Event.cs
public partial class Event : Auditable
{
#region Primitive Properties
public Event()
{
this.Parents = new List<EventRelationship>();
this.Children = new List<EventRelationship>();
this.Properties = new List<EventProperty>();
this.ViewProperties = new List<ViewEventProperty>();
}
public virtual int Id { get; set; }
}
我不确定以上内容与问题的相关性如何,但我想我会把它包含在 cse
ViewModel - EventDetailsViewModel.cs
public class EventDetailsViewModel : IEnumerable<Solution.Domain.Event>
{
public IEnumerable EventId { get; set; }
public IEnumerator<Solution.Domain.Event> GetEnumerator()
{
return GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
}
我根据另一个问题的答案将 GetEnumertor 部分放入,但我不确定它是否正确。
查看 - index.cshtml
@Html.Action("Widget")
我只是在视图中调用上面的内容,我认为不需要任何其他信息(如果需要请告诉我)。
部分视图 - Widget.cshtml
@model IEnumerable<Solution.Web.Models.EventDetailsViewModel>
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title></title>
</head>
<body>
@foreach (var item in Model)
{
<div>@item.EventId</div>
}
HTML.Display
</body>
</html>
我认为上面列出的结果是正确的,但我不是 100%
我的目标
好的,所以我要做的只是列出来自数据库的 EventId,这些 EventId 由 Widget() 子操作在视图中获取。
我的问题
使用我当前的代码,我收到以下错误消息。
The model item passed into the dictionary is of type 'System.Collections.Generic.List
1[<>f__AnonymousType22
1[System.Int32]]', but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable`1[Solution.Web.Models.EventDetailsViewModel]'.
我可以看到错误是说该操作是 returning Generic.List 而我要求的是 IEnumerable 但我不知道如何 return 数据以使其成为这种类型。
所以,有人能帮我解决这个问题吗,因为我现在一事无成...?
这个问题现在已经够长了,所以我会把它留在那里,但如果有人需要更多细节,那就问吧。
谢谢
J
我不明白你为什么要在 EventDetailsViewModel
中实施 IEnumerable<Solution.Domain.Event>
,也许我在这里错过了大局。为了解决模型类型不正确的具体问题,您可以进行这样的更改:
首先,简化视图模型(并将 Id 类型更改为 int):
public class EventDetailsViewModel
{
public int EventId { get; set; }
}
其次 - 在您的控制器方法中使用此类型而不是匿名对象:
public ActionResult Widget()
{
var eventIds = ViewEventPropertyService.Get()
.Select(y => new EventDetailsViewModel { EventId = y.EventId })
.ToList();
return PartialView("Widget", eventIds);
}