3

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.
Erik Funkenbusch
  • 92,674
  • 28
  • 195
  • 291
jmcclure
  • 122
  • 11
  • What model is declared in view `@Html.Partial("_ReturnToSearch")`? –  Apr 27 '15 at 13:39
  • 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`) –  Apr 27 '15 at 13:43
  • @StephenMuecke - it is a shared for link items. @Html.ActionLink("Back to Search", "Index", "Home") – jmcclure Apr 27 '15 at 13:55
  • 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 `PaymentViewModel` –  Apr 27 '15 at 14:00
  • _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 Apr 27 '15 at 14:23
  • I found various issues: 1. The installer that was setup didn't create an event viewer log, so I had to create it. 2. The logging setup for the viewer only wrote the message. I modified the object and moved it over 3. There is an error occurring after the BadCheckSubmittedForPayment is set where it is accessing configurations from database. Because of this, that is why I am getting the above error. – jmcclure Apr 27 '15 at 15:37
  • Possible duplicate of [The model item passed into the dictionary is of type .. but this dictionary requires a model item of type](http://stackoverflow.com/questions/40373595/the-model-item-passed-into-the-dictionary-is-of-type-but-this-dictionary-requ) –  Jan 10 '17 at 05:41

1 Answers1

0

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)
}
BYUDigger
  • 139
  • 7