【发布时间】: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("返回搜索", "索引", "主页")
-
我认为这意味着它没有模型?在这种情况下,您的属性
BadCheckSubmittedForPayment是null(调试您的代码)您应该在PaymentViewModel的无参数构造函数中将属性BadCheckSubmittedForPayment初始化为BadCheckSearchViewModel的新实例 -
_ReturnToSearch 是共享的。 BadCheckSearchViewModel 有一个模型(上面编辑过)。在控制器中,会话变量被转换为该类型,然后将其传递给 PaymentViewModel.BadCheckSubmittedForPayment。会话或最终对象值都可以为空。这是在客户网站上(已编辑),我在搜索中进行了很多次搜索,如果是这样的话,所有的结果都是空的。
标签: asp.net asp.net-mvc-3