0
votes

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...

1
Kinda sounds like some files were not updated. - juunas
@juunas Its updated... the partial view has the correct model specified .. - Kos
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) - user3559349
@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
The code you have shown cannot generate that error. Refer this answer for the possible causes - user3559349

1 Answers

1
votes

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.