【问题标题】:The model item passed into the dictionary is of type, but this dictionary requires a model item of type传入字典的模型项是类型的,但是这个字典需要一个类型的模型项
【发布时间】:2015-07-05 23:54:14
【问题描述】:

问题:行:33

传入字典的模型项的类型为“Public.Web.Models.PaymentViewModel”,但此字典需要“Public.Web.Models.BadCheckSearchViewModel”类型的模型项。 说明:执行当前 Web 请求期间发生未处理的异常。请查看堆栈跟踪以获取有关错误及其源自代码的位置的更多信息。 异常详细信息:System.InvalidOperationException:传入字典的模型项的类型为“Public.Web.Models.PaymentViewModel”,但此字典需要“Public.Web.Models.BadCheckSearchViewModel”类型的模型项。 源错误: 第 31 行: 第 32 行:付款 第 33 行:@Html.Partial("_BadCheckSubmittedForPayment", ((Public.Web.Models.BadCheckSearchViewModel)(Model.BadCheckSubmittedForPayment))) 第 34 行: 第 35 行:@Html.LabelFor(model => model.Payment.ServiceChargeDisplay, new { @class= "label-inline" })

服务器: 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)

控制器:

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 Web Pages、.Net 2 和 .Net 4 Client/Extended
  • MVC 经验级别:初级
  • 到错误发生时,我们已经访问了该站点,进行了搜索。选择条目以访问此视图时,会出错。
  • 我很想通过告诉他们升级到 Windows Server 2012 来解决这个问题,但我不能。
  • 这是客户网站
  • 两个驱动器上的空间小于 1GB。

实际问题:

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

【问题讨论】:

  • 视图@Html.Partial("_ReturnToSearch")中声明了什么模型?
  • 请注意它只是@Html.Partial("_BadCheckSubmittedForPayment", Model.BadCheckSubmittedForPayment),但我怀疑属性BadCheckSubmittedForPayment 为空,这意味着您实际上将PaymentViewModel 传递给部分(不是BadCheckSearchViewModel
  • @StephenMuecke - 它是链接项目的共享。 @Html.ActionLink("返回搜索", "索引", "主页")
  • 我认为这意味着它没有模型?在这种情况下,您的属性BadCheckSubmittedForPaymentnull(调试您的代码)您应该在PaymentViewModel 的无参数构造函数中将属性BadCheckSubmittedForPayment 初始化为BadCheckSearchViewModel 的新实例
  • _ReturnToSearch 是共享的。 BadCheckSearchViewModel 有一个模型(上面编辑过)。在控制器中,会话变量被转换为该类型,然后将其传递给 PaymentViewModel.BadCheckSubmittedForPayment。会话或最终对象值都可以为空。这是在客户网站上(已编辑),我在搜索中进行了很多次搜索,如果是这样的话,所有的结果都是空的。

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


【解决方案1】:

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

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

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-03-15
    相关资源
    最近更新 更多