asp.net - 传入字典的模型项是 type 的,但是该字典需要 type 的模型项

标签 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" })

Server: Windows Server 2003R2 32bit

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)

Controller :

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/List。我认为这不适用,因为我不希望它通过列表进行枚举,或者我认为我不会。
  • 这适用于各种其他机器。我立刻就知道的是:Windows Server 2012、Windows 7。
  • 已安装 MVC 3、ASP.net 网页、.Net 2 和 .Net 4 客户端/扩展
  • MVC 经验级别:初级
  • 当错误发生时,我们已访问该网站并进行了搜索。当选择一个条目来访问此 View 时,会出错。
  • 我很想通过告诉他们升级到 Windows Server 2012 来解决这个问题,但我做不到。
  • 这是客户网站
  • 两个驱动器上的空间均少于 1GB。

实际问题:

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

最佳答案

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

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

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

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

关于asp.net - 传入字典的模型项是 type 的,但是该字典需要 type 的模型项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29897264/

相关文章:

asp.net - 从 SQLException 错误输出异常

asp.net - MVC 3 空查询字符串参数值绑定(bind)为空字符串而不是 null

asp.net-mvc - 如何在 ASP.NET MVC 中使用操作过滤器路由到不同的 View 但使用相同的 URL?

asp.net - 与 regenerateExpiredSessionId ="false"和 regenerateExpiredSessionId ="true".net 的区别

asp.net - 添加/更新 Web 引用时防止在 Reference.cs 中生成代理类

asp.net - HtmlGenericControl 对象引用未设置为对象的实例

asp.net - 将盐存储在代码中而不是数据库中

asp.net-mvc-3 - ASP.NET MVC 3 预编译

c# - 在 C# (asp.net MVC3) 中用逗号和小数格式化数字

asp.net-mvc-3 - Ajax.BeginForm 可以重定向到新页面并传递路由值