0

I have a partial view "_Initial" with the model: @model Release

In the main view I have:

@if (Model == null || Model.Release == null)
{
    @Html.Partial("_Initial")//error here
}
else
{
    @Html.Partial("_Initial", Model.Release)
}

The main view has the model: @model WorkspaceData and I can't phantom why am I getting the error:

The model item passed into the dictionary is of type 'WNCT_Web_Application.BL.Workspace.Monitoring.WorkspaceData', but this dictionary requires a model item of type 'WNCT_Web_Application.BL.Release.Release'.

When I'm not even sending a model (the partial view has logic for when the model is null)?

Mihai Bratulescu
  • 1,915
  • 3
  • 27
  • 43

3 Answers3

4

By default if you do not specify a model, or the model you are passing is null, then Html.Partial uses the model from ViewDataDictionary (in your case WorkspaceData. To overcome this, you can pass a new instance of Release to the partial

@Html.Partial("_Initial", new WNCT_Web_Application.BL.Release.Release())
  • 1
    No, I must pass `null`. – Mihai Bratulescu May 05 '15 at 11:30
  • 1
    As you have discovered from the duplicate you can pass `new ViewDataDictionary()` but this suggests you have a (minor) design flaw. Any HtmlHelper in the partial view will internally throw an exception (its all handled gracefully of course) which is why you should always avoid passing a null model. It would be better to check for (say) a default `ID` property - `if (model.ID == 0)` or `if (model.ID == null)` –  May 05 '15 at 11:39
  • Do the internal exceptions affect performance? – Mihai Bratulescu May 05 '15 at 11:55
  • 1
    Yes, just as any exception is, but it minor. Just wanted to make you aware that its better to pass a model if possible (just as you should always use `return View(model)` instead of `return View()` in a controller method) –  May 05 '15 at 11:59
  • And only if the view has no model type defined I must use `return View()`? – Mihai Bratulescu May 05 '15 at 12:16
  • You can still use `return View()` even of a model is defined (say in a 'Create' method), its just that its better to initialize a new instance of your model and pass it to the view. –  May 05 '15 at 12:22
0

You have a design issue here. Rather than one partial view named _Initial, it looks like you need two partials views

  • One to use when Model or Model.Release is null
  • One to use when Model.Release is not null

For example:

@if (Model == null || Model.Release == null)
{
    @Html.Partial("_InitialNullRelease")
}
else
{
    @Html.Partial("_Initial", Model.Release)
}

Obviously you can choose a better name than what I've given.

Depending on how complicated _Initial actually is, or what's it's meant to do, splitting the view into two might be a good approach.

Jason Evans
  • 28,906
  • 14
  • 90
  • 154
  • `_Initial` isn't complex, it just makes some checks with the `Release` sent and the releases in the application to color code it red in the UI if it was deleted from the application. I don't think I should create 2 views since it will only duplicate code. – Mihai Bratulescu May 05 '15 at 11:29
0

Solution: Html.Partial("_Initial", null, new ViewDataDictionary())

Thanks Stephen Muecke for telling me about ViewDataDictionary and how it sends the model of the view to the partial if no model was given to the Html.Partialmethod.

Mihai Bratulescu
  • 1,915
  • 3
  • 27
  • 43