Reputation: 15978
I would like to render partial like this:
@Html.Partial("_MyPartial")
However my partial does need a typed object, for instance: MyPartial.cshtml :
@model MyApplication.Models.MyClass
(...)
But I would like to render it on my view WITHOUT a initialized model, like a "Create" view.
Create views is bound to a model, but is initialized empty.
What I already tried:
@Html.Partial("_MyPartial")
The main view model is passed to the partial and it throws an incompatible types error.
@Html.Partial("_MyPartial", null)
throws same error.
@Html.Partial("_MyPartial", new MyClass())
initializes view with default values. I don't want pre initialized view.
In a "Create" view, no model is passed to the view, BUT the view is bound to a model. I would like to render this partial in a "create" view. It must no be bound to a initialized model instance. It must be initialized empty.
Upvotes: 1
Views: 6252
Reputation: 15978
In the fact, this is not possible, cause partial gets the model from the main view, if there is no one from itself.
In this case you must initialize the call partial and pass not only the model as null
but the entire data from the context must be cleaned.
Upvotes: 2
Reputation: 350
When you want to return a partialview with no model. Use this.
@{ Html.RenderPartial("Your partial view") }
Upvotes: 0
Reputation: 14679
You have already write code to accept model type in view:
@model MyApplication.Models.MyClass
So it is compulsory that you have to pass model in view.
If you don't wants to use model then remove your code which accept model
Upvotes: 1