传递到字典中的模型项是类型,但此字典需要类型的模型项

时间:2015-04-27 13:33:12

标签: asp.net asp.net-mvc-3

问题:行:33

    The model item passed into the dictionary is of type 'Public.Web.Models.PaymentViewModel', but this dictionary requires a model item of type 'Public.Web.Models.BadCheckSearchViewModel'. 
    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 'Public.Web.Models.PaymentViewModel', but this dictionary requires a model item of type 'Public.Web.Models.BadCheckSearchViewModel'.

    Source Error: 


    Line 31:                    
    Line 32:                        Payment for
    Line 33:                        @Html.Partial("_BadCheckSubmittedForPayment", ((Public.Web.Models.BadCheckSearchViewModel)(Model.BadCheckSubmittedForPayment)))
    Line 34:                         
    Line 35:                        @Html.LabelFor(model => model.Payment.ServiceChargeDisplay, new { @class = "label-inline" })

服务器: Windows Server 2003R2 32位

Index.cshtml

@using BootstrapSupport
@model Public.Web.Models.PaymentViewModel

@{
    ViewBag.Title = "Index";
    Layout = "~/Views/Shared/_BootstrapLayout.basic.cshtml";
}
<style type="text/css">
    .form-condensed .control-group {
        margin-top: 0;
        margin-bottom: 5px;
    }
    .label-inline {
        display: inline;
    }
</style>
@Html.Partial("_ReturnToSearch")
@using (Html.BeginForm("SubmitPayment", "Payment", FormMethod.Post, new { @class = "form-horizontal form-condensed", id = "paymentform" }))
                {
                    @Html.AntiForgeryToken()
                    @Html.ValidationSummary(true)
    <div class="container">

        <div class="row">
            <div class="span4">
                @*@using (Html.BeginForm("UpdatePayment", "Payment", FormMethod.Post, new { @class = "form-horizontal form-condensed", id = "partialpaymentform"  }))
                {
                    @Html.AntiForgeryToken()
                    @Html.ValidationSummary(true)
                *@<div class="well">
                    <fieldset>
                        <legend>Payment for</legend>
                        @Html.Partial("_BadCheckSubmittedForPayment", (Public.Web.Models.BadCheckSearchViewModel)Model.BadCheckSubmittedForPayment)

控制器:

public ActionResult Index()
        {
            var paymentViewModel = new PaymentViewModel();
            try
            {
                if (Session["SubmittedBadCheckForPayment"] != null)
                {
                    BadCheckSearchViewModel submittedForPayment = Session["SubmittedBadCheckForPayment"] as BadCheckSearchViewModel;
                    if (submittedForPayment != null)
                    {
                        var uri = System.Web.Configuration.WebConfigurationManager.AppSettings["BadCheckServiceUrl"];
                        _ctx = new Public.Web.BadChecks.CmsPublicEntities(new Uri(uri));

                        paymentViewModel.BadCheckSubmittedForPayment = submittedForPayment;
//.........
                    }
                }
            }
            catch (Exception ex)
            {
//....
            }

            return View(paymentViewModel);
        }

模型

public class PaymentViewModel
{
    private BadCheckSearchViewModel _badCheckSubmittedForPayment;

    public BadCheckSearchViewModel BadCheckSubmittedForPayment
    {
        get
        {
            return _badCheckSubmittedForPayment;
        }
        set
        {
            _badCheckSubmittedForPayment = value;
        }
    }

}

型号:BadChecks

public class BadCheckSearchViewModel
    {
        private Double? originalAmount;

        public string ApplicationCode { get; set; }

        public decimal CaseId { get; set; }

        public decimal PartySequence { get; set; }

        public string LastName { get; set; }

        public string FirstName { get; set; }

        public string MiddleName { get; set; }

        public string FullName { get; set; }
  • 我看过他们说要制作@model声明的其他帖子 IEnumerable的/列表。我不认为这是适用的,因为我不希望它通过列表列举,或者我不认为我是。
  • 这适用于其他各种机器。我所知道的是:Windows Server 2012,Windows 7。
  • 安装了MVC 3,ASP.net网页,.Net 2和.Net 4 Client / Extended
  • MVC的经验水平:Entry-Junior
  • 当错误发生时,我们已经访问了该网站,进行了搜索。选择访问此视图的条目时,会出错。
  • 我很乐意通过告诉他们升级到Windows Server 2012来解决这个问题,但我无法解决。
  • 这是客户网站
  • 两个驱动器上的空间不到1GB。

实际问题:

    paymentViewModel.BadCheckSubmittedForPayment = submittedForPayment;
    paymentViewModel.Payment.ShowEmailIfConfiguredToSendEmail = _cmsPublicSettings.ShowEmailAddressOnPaymentForm;  //Object Reference Error Here on this property.

1 个答案:

答案 0 :(得分:0)

正如Stephen在评论中指出的那样,这是由Model.BadCheckSubmittedForPayment null引起的。

当这是null时,它尝试仅使用视图名称调用Html.Partial()函数,并且默认为传入此文件的基本视图模型。在您的情况下@model Public.Web.Models.PaymentViewModel

您可以通过使用空检查(请参阅代码)或确保已初始化来解决此问题。

if (Model.BadCheckSubmittedForPayment != null)
{
     @Html.Partial("_BadCheckSubmittedForPayment", (Public.Web.Models.BadCheckSearchViewModel)Model.BadCheckSubmittedForPayment)
}