gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 21:08:29 25 4
gpt4 key购买 nike

问题:行: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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com