The model item passed to the dictionary is of type, but this dictionary requires a model item of type

Problem: 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've seen other posts where they say to make @model IEnumerable / List declaration. I don't think this is applicable because I don't expect it to list the list, or I don't think it is.
  • This works on other machines. I know what's on my head: Windows Server 2012, Windows 7.
  • Installed MVC 3, ASP.net, .Net 2 and .Net 4 Client / Extended webpages.
  • Experience level with MVC: Entry-Junior
  • By the time the error occurred, we had access to the search site. There are errors when selecting a record to access this view.
  • I would like to solve this problem by suggesting they upgrade to Windows Server 2012, but I cannot.
  • This is the client's site
  • Less than 1 g of space on both drives.

Actual problem:

    paymentViewModel.BadCheckSubmittedForPayment = submittedForPayment;
    paymentViewModel.Payment.ShowEmailIfConfiguredToSendEmail = _cmsPublicSettings.ShowEmailAddressOnPaymentForm;  //Object Reference Error Here on this property.

      

+3


source to share


1 answer


As Stephen pointed out in the comments, this is caused Model.BadCheckSubmittedForPayment

null

.

When it does null

, it tries to call the function Html.Partial()

with just the view name, and by default is the base view model pass for that file. In your case@model Public.Web.Models.PaymentViewModel



You can fix this with a null check (see code) or make sure it was initialized.

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

      

0


source







All Articles