H. Pauwelyn
H. Pauwelyn

Reputation: 14330

How to solve "model passed into the dictionary is of type string but requires object" just on server, local host okay

I've created a partial view that I reuse twice. I call this partial view with code below:

@Html.Partial("_TekstBewerker", Model.Text)

The property Model.Text is a string and is not null. On image below you could find the code of my partial view left the current version on the server and right what the code is after publishing.

You see the code is two times the same but on the server I've got this exception:

InvalidOperationException: The model item passed into the dictionary is of type String, but this dictionary requires a model item of type NieuwBlogViewModel.

Before publishing the the new version of my partial view, the model was indeed NieuwBlogViewModel and not String. This must be updated but it isn't.

On local host I've no problems with this code.

Oh yeah, almost forgotten to say that I've replaced the partial view to the Shared folder instead of the Admin folder. This last folder was the previous location of my partial view.

Could this be the problem of the exception and how could I solve this on the server?

Upvotes: 0

Views: 71

Answers (2)

Darin Dimitrov
Darin Dimitrov

Reputation: 1039588

It seems that when you moved the partial from one place to the other, the old file remained somehow on the server - it didn't get deleted. And since ASP.NET MVC scans the folders in order using a convention, it picks the old file first. Make sure it gets deleted on the server from the old location. When you explicitly specified the location of the file, then ASP.NET MVC will directly use the new file and it is the reason why it works but in general it's better to rely on the convention.

Upvotes: 1

H. Pauwelyn
H. Pauwelyn

Reputation: 14330

By a comment of @BalajiMarimuthu I found the correct solution. Now I use this code:

@Html.Partial("~/Views/Folder/ViewName.cshtml", Model.text)

Now I've just one question: Why the situation in my question will not work on the server?

Upvotes: 0

Related Questions