0

I have the following class which encapsulate a list of SelectListItem and an instance of ViewModel

public class SwitchCampaignCapsule
{
    public TrackEmployeeSwitchStageViewModel ViewModel { get; set; }

    public List<SelectListItem> Stages { get; set; }
}

Then I have a presentation class which encapsulate the SwitchCampaignCapsule along with other stuff that I removed for the sake of the simplicity.

public class TrackPresenter
{
    public SwitchCampaignCapsule SwitchCampaignCapsule { get; set; }
    ....
    ....
    ....
}

I pass the Proj.TrackPresenter class to my main view. Then in the main view I am trying to pass the Model.SwitchCampaignCapsule in a partial view.

Here is how my main view looks like

@model Proj.TrackPresenter
@{
    ViewBag.Title = "Time Tracker - Track";
}

@Html.Partial("Partials/_TrackSwitchStage", Model.SwitchCampaignCapsule)

....
....
....

and here is my partial view

@model Proj.Capsules.SwitchCampaignCapsule

    @using (Html.BeginForm("SwitchStage", "Track", FormMethod.Post, new { @class = "form-horizontal" }))
    {
        @Html.AntiForgeryToken()
        @Html.HiddenFor(m => m.ViewModel.From, new { @class = "tracker-selected-date" })
        @Html.HiddenFor(m => m.ViewModel.LocalFrom, new { @class = "tracker-local-selected-date" })

        <div class="form-group">
            @Html.LabelFor(m => m.ViewModel.StageId, new { @class = "col-sm-2 control-label" })
            <div class="col-sm-10">
                @Html.DropDownListFor(m => m.ViewModel.StageId, Model.Stages, new { @Class = "form-control" })
                @Html.ValidationMessageFor(m => m.ViewModel.StageId)
            </div>
        </div>

        <div class="form-group">

            <div class="col-sm-offset-2 col-sm-10">
                <button type="submit" class="btn btn-primary style-my-form-for-validation" id="TrackSwitchStageSubmit">Switch stage</button>
            </div>

        </div>

    }

But when I run the application, I get the following error

The model item passed into the dictionary is of type 'Proj.TrackPresenter', but this dictionary requires a model item of type 'Proj.Capsules.SwitchCampaignCapsule'.

I am not sure why am I getting this error. it is very clear that I am passing Model.SwitchCampaignCapsule to the partial view and not Model

What am I missing here? Why am I getting this error? How can I fix it?

Jaylen
  • 39,043
  • 40
  • 128
  • 221
  • 1
    It means that your `SwitchCampaignCapsule` property is `null` (and by default it will then pass the model in the main view to the partial). Make sure you initialize `SwitchCampaignCapsule` to a new instance before passing the `TrackPresenter` model to the view –  Sep 16 '16 at 01:52
  • @StephenMuecke Thank you so much. I did not know that if the property is null it will automatically try to pass the model itself. If you post an answer I will accept it. – Jaylen Sep 16 '16 at 02:05
  • @Jaylen Suggesting to post answer to post with plenty of well-known easy to find duplicates is not really follows spirit of SO... – Alexei Levenkov Sep 16 '16 at 02:14

0 Answers0