0

I have the following situation:

Controller:

[HttpGet]
public async Task<IActionResult> Action()
{
     var actionViewModel = new ActionViewModel()
     {
          ActionOptions = new ActionOptionsViewModel()
     }

     return View(actionViewModel)
}

View:

@model ActionViewModel

@{ Html.RenderPartial("_ActionOptions", Model.ActionOptions ); }

Partial View:

@model ActionOptionsViewModel

The exception I am getting is:

InvalidOperationException: The model item passed into the ViewDataDictionary is of type 'ActionOptionsViewModel', but this ViewDataDictionary instance requires a model item of type 'ActionViewModel'.

Weirdly enough I get this error only when I get deployed this code on Azure but it works locally. Any ideas why this is happening?! I cant reproduce the error on localhost and cant figure out what possible could be different when it comes to rendering a view on localhost and Azure...

Kos
  • 567
  • 4
  • 15
  • Kinda sounds like some files were not updated. – juunas Jan 06 '18 at 17:56
  • @juunas Its updated... the partial view has the correct model specified .. – Kos Jan 06 '18 at 18:39
  • Since the code you have shown cannot possibly generate that error, best guess is that it's due to code in a POST method where you return a view because `ModelState` is invalid (and you return `ActionOptionsViewModel` to the main view which expects `ActionViewModel`) –  Jan 06 '18 at 20:09
  • @StephenMuecke I dont get to the post method. The error happens when hitting the get and then in the returned view. If I intentionally change the partial view model to 'ActionViewModel' then I get the same error locally. – Kos Jan 06 '18 at 22:19
  • The code you have shown cannot generate that error. Refer [this answer](https://stackoverflow.com/questions/40373595/the-model-item-passed-into-the-dictionary-is-of-type-but-this-dictionary-requ) for the possible causes –  Jan 06 '18 at 22:23
  • @StephenMuecke I am creating the models before passing them to the view using the “new” keyword so they can not be null and I don’t get the exception when running on localhost it happens only on Asure. so I was wondering what could be different when it renders the view on Azure – Kos Jan 06 '18 at 22:48
  • The error states your passing an instance of `ActionOptionsViewModel` to a view/partial view (or layout) which has `@model ActionViewModel` which is not the code you have shown, so I can only assume you have omitted some relevant code (note also that I have seen issues with naming a method `Action` although probably not related in your case) –  Jan 06 '18 at 22:56

1 Answers1

1

I cant reproduce the error on localhost and cant figure out what possible could be different when it comes to rendering a view on localhost and Azure

I would recommend you empty your web content on Azure and republish your application to azure to narrow this issue. For Azure Web App, you could leverage KUDU or connect to your site via ftp to empty your web content under D:\home\site\wwwroot folder. Also, you could Remote debugging web apps. For Azure Cloud Service or Azure VM, you could remote to your server and empty the web content folder.

Bruce Chen
  • 18,207
  • 2
  • 21
  • 35