簡體   English   中英

傳遞到字典中的模型項屬於類型,但此字典需要類型為“System.Collections.Generic.IEnumerable”的模型項

[英]The model item passed into the dictionary is of type, but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable

這就是我想要做的。 我不明白為什么它需要一個可枚舉的列表。

地址控制器.cs

public ActionResult ShowAddresses(List<Address> ReturnAddresses)
    {
        ShowAddressViewModel viewModel = new ShowAddressViewModel() { Addresses = ReturnAddresses, Message = "New" };
return PartialView("_ShowAddr", viewModel);

}

ShowAddressViewModel.cs

public class ShowAddressViewModel
{
    public List<Address> Addresses { get; set; }
    public string Message { get; set; }
}

_ShowAddr.cshtml

@model PeopleSoftControlsPOC.Models.ShowAddressViewModel
<script type="text/javascript">

</script>
<form>
<div class="addressBlock">
<table id="AddressTable">
    @{int i = 0;}
     @{PeopleSoftControlsPOC.Models.ShowAddressViewModel AddrModel = Model;}
    @foreach (var item  in AddrModel.Addresses)
    {
        <tr id = "@(i)">
            <td>@(AddrModel.Addresses[i].Address1)
            </td>
            <td>@(AddrModel.Addresses[i].Address2)
            </td>
            <td>@(AddrModel.Addresses[i].City)
            </td>
            <td>@(AddrModel.Addresses[i].State)
            </td>
            <td>@(AddrModel.Addresses[i].Zip)
            </td>
        </tr>
        @(i++)
    }
</table>
</div>
</form>

編輯

從另一個局部視圖的 java 腳本調用

$.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 :(');
            }
        });
    }

“/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 

假設您正在嘗試使用

@Html.Partial("_ShowAddr", Model.Adr)

並且您的模型無需通過控制器即可發送到視圖,因此您實際上是向視圖發送地址集合而不是包裝的視圖模型。

試試這個方法:

@{ Html.RenderAction("ShowAddresses", Model.Adr); }

編輯

其實:

@{ Html.RenderAction("TestPart", new { ReturnAddresses = Model.Adr }); }

我認為是

 return PartialView("_ShowAddr", viewModel.ToList());

並在你的_ShowAddr.cshtml頂部

 @model IEnumerable<PeopleSoftControlsPOC.Models.ShowAddressViewModel>

我在做像你一樣的事情

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM