C# 传递到字典中的模型项的类型为,但此字典需要类型为';System.Collections.Generic.IEnumerable

C# 传递到字典中的模型项的类型为,但此字典需要类型为';System.Collections.Generic.IEnumerable,c#,jquery,asp.net-mvc,razor,C#,Jquery,Asp.net Mvc,Razor,这就是我要做的。我不明白为什么它需要一个可枚举的列表 AddressController.cs public ActionResult ShowAddresses(List<Address> ReturnAddresses) { ShowAddressViewModel viewModel = new ShowAddressViewModel() { Addresses = ReturnAddresses, Message = "New" }; return

这就是我要做的。我不明白为什么它需要一个可枚举的列表

AddressController.cs

public ActionResult ShowAddresses(List<Address> ReturnAddresses)
    {
        ShowAddressViewModel viewModel = new ShowAddressViewModel() { Addresses = ReturnAddresses, Message = "New" };
return PartialView("_ShowAddr", viewModel);
“/PSPOC”应用程序中出现服务器错误。 --------------------------------------------------------------------------------

The model item passed into the dictionary is of type 'PeopleSoftControlsPOC.Models.ShowAddressViewModel', but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable`1[PeopleSoftControlsPOC.Models.ShowAddressViewModel]'. 
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: System.InvalidOperationException: The model item passed into the dictionary is of type 'PeopleSoftControlsPOC.Models.ShowAddressViewModel', but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable`1[PeopleSoftControlsPOC.Models.ShowAddressViewModel]'.

Source Error: 

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.  


Stack Trace: 


[InvalidOperationException: The model item passed into the dictionary is of type 'PeopleSoftControlsPOC.Models.ShowAddressViewModel', but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable`1[PeopleSoftControlsPOC.Models.ShowAddressViewModel]'.]
   System.Web.Mvc.ViewDataDictionary`1.SetModel(Object value) +383
   System.Web.Mvc.ViewDataDictionary..ctor(ViewDataDictionary dictionary) +625
   System.Web.Mvc.WebViewPage`1.SetViewData(ViewDataDictionary viewData) +74
   System.Web.Mvc.RazorView.RenderView(ViewContext viewContext, TextWriter writer, Object instance) +138
   System.Web.Mvc.ViewResultBase.ExecuteResult(ControllerContext context) +378
   System.Web.Mvc.<>c__DisplayClass1c.<InvokeActionResultWithFilters>b__19() +33
   System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilter(IResultFilter filter, ResultExecutingContext preContext, Func`1 continuation) +727120
   System.Web.Mvc.ControllerActionInvoker.InvokeActionResultWithFilters(ControllerContext controllerContext, IList`1 filters, ActionResult actionResult) +265
   System.Web.Mvc.ControllerActionInvoker.InvokeAction(ControllerContext controllerContext, String actionName) +727076
   System.Web.Mvc.Controller.ExecuteCore() +159
   System.Web.Mvc.ControllerBase.Execute(RequestContext requestContext) +334
   System.Web.Mvc.<>c__DisplayClassb.<BeginProcessRequest>b__5() +62
   System.Web.Mvc.Async.<>c__DisplayClass1.<MakeVoidDelegate>b__0() +15
   System.Web.Mvc.<>c__DisplayClasse.<EndProcessRequest>b__d() +52
   System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +606
   System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +288



--------------------------------------------------------------------------------
Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.0.30319.18044 
传递到字典中的模型项的类型为“PeopleSoftControlsPOC.Models.ShowAddressViewModel”,但此字典需要类型为“System.Collections.Generic.IEnumerable”的模型项“1[PeopleSoftControlsPOC.Models.ShowAddressViewModel]”。
描述:执行当前web请求期间发生未处理的异常。请查看堆栈跟踪以了解有关错误的更多信息以及错误在代码中的起源。
异常详细信息:System.InvalidOperationException:传递到字典的模型项的类型为“PeopleSoftControlsPOC.Models.ShowAddressViewModel”,但此字典需要类型为“System.Collections.Generic.IEnumerable”的模型项“1[PeopleSoftControlsPOC.Models.ShowAddressViewModel]”。
源错误:
在执行当前web请求期间生成了未经处理的异常。有关异常的起源和位置的信息可以使用下面的异常堆栈跟踪来识别。
堆栈跟踪:
[InvalidOperationException:传入字典的模型项的类型为'PeopleSoftControlsPOC.Models.ShowAddressViewModel',但此字典需要'System.Collections.Generic.IEnumerable'1[PeopleSoftControlsPOC.Models.ShowAddressViewModel]类型的模型项。]
System.Web.Mvc.ViewDataDictionary`1.SetModel(对象值)+383
System.Web.Mvc.ViewDataDictionary..ctor(ViewDataDictionary)+625
System.Web.Mvc.WebViewPage`1.SetViewData(ViewDataDictionary viewData)+74
System.Web.Mvc.RazorView.RenderView(ViewContext、TextWriter、Object实例)+138
System.Web.Mvc.ViewResultBase.ExecuteResult(ControllerContext上下文)+378
System.Web.Mvc.c__显示Class1C.b__19()+33
System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilter(IResultFilter筛选器,ResultExecutingContext预文本,Func`1 continuation)+727120
System.Web.Mvc.ControllerActionInvoker.InvokeActionResultWithFilters(ControllerContext ControllerContext,IList`1 filters,ActionResult ActionResult)+265
System.Web.Mvc.ControllerActionInvoker.InvokeAction(ControllerContext ControllerContext,String actionName)+727076
System.Web.Mvc.Controller.ExecuteCore()+159
System.Web.Mvc.ControllerBase.Execute(RequestContext-RequestContext)+334
System.Web.Mvc.c__显示类b.b__5()+62
System.Web.Mvc.Async.c__显示类1.b__0()+15
System.Web.Mvc.c_uuudisplayClasse.b_uuud()+52
System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()+606
System.Web.HttpApplication.ExecuteStep(IExecutionStep步骤,布尔值&同步完成)+288
--------------------------------------------------------------------------------
版本信息:Microsoft.NET Framework版本:4.0.30319;ASP.NET版本:4.0.30319.18044

在_ShowAddr.cshtml中,您不需要为列表编制索引。该模型是PeopleSoftControlsPC.Models.ShowAddressViewModel,因此您应该能够简单地执行以下操作:

@foreach (var item in Model.Addresses)
{
  <tr>
    <td>@item.Address1</td>
    <td>@item.Address2</td>
    <td>@item.City</td>
    <td>@item.State</td>
    <td>@item.Zip
  </tr>
}
@foreach(Model.Addresses中的变量项)
{
@项目1.地址1
@项目1.2地址
@项目.城市
@项目.说明
@物品。拉链
}

POST操作代码在哪里?我觉得它看起来很正确。。。您确定
\u ShowAddr.cshtml
是您将
ShowAddressViewModel
传递给的唯一视图吗?@neoist一篇文章具有调用控制器方法ShowAddress的url,如上面控制器代码所示。我正在使用ShowAddressViewModel从另一部分视图的javascript调用另一个外部服务,该部分视图返回围绕ShowAddressViewModel的JSON可能有点离题,但我可以问一下,为什么在foreach循环中使用ant递增int值,而不是仅仅递增id,这是荒谬的?
$.ajax(url, {
            data: { ReturnAddresses : InboundAddresses },
            type: 'POST',
            cache: false,
            crossDomain: true,
            success: function (data) {
                //Populate the form values
                // Start Dialog Code
                $myWindow = jQuery('#myDiv');
                //instantiate the dialog
                $myWindow.html(data);
                $myWindow.dialog({
                    title: 'Select an address',
                    modal: true,
                    width: 'auto'
                });
                $myWindow.show();
                $myWindow.dialog("open");
                // End Dialog Code

                $('#AddressTable').on('click', 'tr', function () {
                    alert('You clicked row ' + ($(this).index()));
                });
                addAddress(Addresses, Message)
            },
            error: function (jqXHR, textStatus, errorThrown) {
                $('#Message').val('Kaboom!!! (The call blew up...#thatsucks)');
                alert('The Dialog Box call failed...Sorry :(');
            }
        });
    }
The model item passed into the dictionary is of type 'PeopleSoftControlsPOC.Models.ShowAddressViewModel', but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable`1[PeopleSoftControlsPOC.Models.ShowAddressViewModel]'. 
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: System.InvalidOperationException: The model item passed into the dictionary is of type 'PeopleSoftControlsPOC.Models.ShowAddressViewModel', but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable`1[PeopleSoftControlsPOC.Models.ShowAddressViewModel]'.

Source Error: 

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.  


Stack Trace: 


[InvalidOperationException: The model item passed into the dictionary is of type 'PeopleSoftControlsPOC.Models.ShowAddressViewModel', but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable`1[PeopleSoftControlsPOC.Models.ShowAddressViewModel]'.]
   System.Web.Mvc.ViewDataDictionary`1.SetModel(Object value) +383
   System.Web.Mvc.ViewDataDictionary..ctor(ViewDataDictionary dictionary) +625
   System.Web.Mvc.WebViewPage`1.SetViewData(ViewDataDictionary viewData) +74
   System.Web.Mvc.RazorView.RenderView(ViewContext viewContext, TextWriter writer, Object instance) +138
   System.Web.Mvc.ViewResultBase.ExecuteResult(ControllerContext context) +378
   System.Web.Mvc.<>c__DisplayClass1c.<InvokeActionResultWithFilters>b__19() +33
   System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilter(IResultFilter filter, ResultExecutingContext preContext, Func`1 continuation) +727120
   System.Web.Mvc.ControllerActionInvoker.InvokeActionResultWithFilters(ControllerContext controllerContext, IList`1 filters, ActionResult actionResult) +265
   System.Web.Mvc.ControllerActionInvoker.InvokeAction(ControllerContext controllerContext, String actionName) +727076
   System.Web.Mvc.Controller.ExecuteCore() +159
   System.Web.Mvc.ControllerBase.Execute(RequestContext requestContext) +334
   System.Web.Mvc.<>c__DisplayClassb.<BeginProcessRequest>b__5() +62
   System.Web.Mvc.Async.<>c__DisplayClass1.<MakeVoidDelegate>b__0() +15
   System.Web.Mvc.<>c__DisplayClasse.<EndProcessRequest>b__d() +52
   System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +606
   System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +288



--------------------------------------------------------------------------------
Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.0.30319.18044 
@foreach (var item in Model.Addresses)
{
  <tr>
    <td>@item.Address1</td>
    <td>@item.Address2</td>
    <td>@item.City</td>
    <td>@item.State</td>
    <td>@item.Zip
  </tr>
}