问题:行: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; }
实际问题:
paymentViewModel.BadCheckSubmittedForPayment = submittedForPayment;
paymentViewModel.Payment.ShowEmailIfConfiguredToSendEmail = _cmsPublicSettings.ShowEmailAddressOnPaymentForm; //Object Reference Error Here on this property.
答案 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)
}