5

Problem: Error message upon passing viewmodel to partial view.

Main page: Index.cshmtl, uses class DivisionModel

@model  DivisionViewModel

@{
  Layout = "~/Views/Shared/_Layout.cshtml";
}



@*<h2>Division</h2>*@
<div>

    <!-- Nav tabs -->
    <ul class="nav nav-tabs" role="tablist" id="divTabs">
        <li role="presentation" class="active"><a href="#home" aria-controls="home" role="tab" data-toggle="tab">Home</a></li>

    </ul>

    <!-- Tab panes -->
    <div class="tab-content">

        <!-- Home -->
        <div role="tabpanel" class="tab-pane active" id="home">
            @Html.Partial("~/Views/Company/Division/_prtDivision.cshtml", new addDivisionViewModel())
        </div>

Partial view: _prtDivision.cshmtl, uses addDivisionViewModel

@model addDivisionViewModel

@{
    Layout = "~/Views/Company/Division/Index.cshtml";
}

@{
  ViewBag.Title = "Create";
}

<h2>add division</h2>

@using (Html.BeginForm("addDivision", "Division", FormMethod.Post))
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
    <hr />

    <div class="row">
        <div class="col-sm-12">
            @Html.ValidationSummary(false, string.Empty, new { @class = "text-danger" })
        </div>
    </div>

However, when I pass 'addDivionsViewModel to the view.

i.e. on index page in the tabpanel

@Html.Partial("~/Views/Company/Division/_prtDivision.cshtml", new addDivisionViewModel())

I get the below error message:

enter image description here

Indicating the passed model is incorrect... If I then change the calling method to:

@Html.Partial("~/Views/Company/Division/_prtDivision.cshtml", new DivisionViewModel())

or

@Html.Partial("~/Views/Company/Division/_prtDivision.cshtml", Model)

I get the below message:

enter image description here

So, irrespective of the what model I send to the partial view, VS tells me it wants the opposite one ?

I've been digging around a lot on this topic, for something that would appear relatively simple it doesn't really turn out to be all that simple...

What I've tried:

  • Adding addDivisionViewModel as property of DivisionModel and passing Model.addDivisionViewModel to _prtDivision.cshtml
  • Following above but assigning the property to created variable just before sending (i.e. @{ var m = Model.addDivisionViewModel})
  • Passing Model and separating the .addDivisionViewModel from model. Loosing functionality and loosing references to original data. I.e. data cannot be displayed (too deep into tree structure => Model.addDivisonViewModel.Division.DivisionName) and about a hundred other methods seen on the web...

Any help will be appreciated

mtholen
  • 1,631
  • 2
  • 15
  • 27

2 Answers2

1

Your partial view has a model which is addDivisionViewModel but it also includes

Layout = "~/Views/Company/Division/Index.cshtml";

and the Index.cshtml view has a model which is DivisionViewModel

It cannot be both (unless one is deriving from the other). Remove the Layout = ".." line from your partial view (a partial view should not have a layout)

  • Isn't it funny how these things are so very often so very simple...:-) cheers – mtholen Mar 18 '17 at 22:49
  • I wrote [this community wiki answer](http://stackoverflow.com/questions/40373595/the-model-item-passed-into-the-dictionary-is-of-type-but-this-dictionary-requ) thinking I had covered every possibility for generating that error but I was wrong :) –  Mar 18 '17 at 22:52
0

Make your partialviewLayout as null

@model addDivisionViewModel

@{
    Layout = null;
}
Vadim Martynov
  • 8,602
  • 5
  • 31
  • 43
cpr43
  • 2,942
  • 1
  • 18
  • 18