3
votes

Issue: Line: 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);
        }

Model:

public class PaymentViewModel
{
    private BadCheckSearchViewModel _badCheckSubmittedForPayment;

    public BadCheckSearchViewModel BadCheckSubmittedForPayment
    {
        get
        {
            return _badCheckSubmittedForPayment;
        }
        set
        {
            _badCheckSubmittedForPayment = value;
        }
    }

}

Model: 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; }
  • I have seen other posts where they say to make the @model declaration IEnumerable/List. I do not think this applies because I am not expecting it to enumerate through a list or I don't think I am.
  • This works on various other machines. The ones I know off the top of my head are: Windows Server 2012, Windows 7.
  • MVC 3, ASP.net Web Pages, .Net 2 and .Net 4 Client/Extended is installed
  • Experience Level with MVC: Entry-Junior
  • By the time the error has occured, we have accessed the site, searched. When selecting an entry to access this view, it errors.
  • I would love to solve this problem by telling them to upgrade to Windows Server 2012, but I can't.
  • This is customer site
  • Less than 1gb of space on both drives.

Actual Issue:

    paymentViewModel.BadCheckSubmittedForPayment = submittedForPayment;
    paymentViewModel.Payment.ShowEmailIfConfiguredToSendEmail = _cmsPublicSettings.ShowEmailAddressOnPaymentForm;  //Object Reference Error Here on this property.
1
What model is declared in view @Html.Partial("_ReturnToSearch")?user3559349
Note also its just @Html.Partial("_BadCheckSubmittedForPayment", Model.BadCheckSubmittedForPayment), but I suspect that property BadCheckSubmittedForPayment is null which means that you actually passing PaymentViewModel to the partial (not BadCheckSearchViewModel)user3559349
@StephenMuecke - it is a shared for link items. @Html.ActionLink("Back to Search", "Index", "Home")jmcclure
I assume that means that it has no model? In which case your property BadCheckSubmittedForPayment is null (debug you code) You should initialize property BadCheckSubmittedForPayment to a new instance of BadCheckSearchViewModel in a parameterless constructor for PaymentViewModeluser3559349
_ReturnToSearch is the shared. There is a model for BadCheckSearchViewModel (edited above). In the controller, the session variable is converted to that type, and then it is passed to PaymentViewModel.BadCheckSubmittedForPayment. Either the session or the final object value could be null. This is on customer site (edited), and I have went through a good many in the search and all come back as null, if that is the case.jmcclure

1 Answers

0
votes

As pointed out by Stephen in the comments this is caused by Model.BadCheckSubmittedForPayment being null.

When this is null it tries to call the Html.Partial() function with just the view name and defaults to passing in the base view model for this file. In your case @model Public.Web.Models.PaymentViewModel

You can fix this by surrounding with a null check (see code) or ensuring it has been initialized.

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