newbie_86
newbie_86

Reputation: 4610

razor partial view to cater for different model types

Is it possible for an mvc razor partial view to take in one of 2 models....I have a create template and an edit template, they're similar but not the same. So i've created a common partial view, extracted the similar items into that, but it requires a model. I want this partial to be able to take in either a create model or edit model depending on where it's being called from.

So in my create view i would say @{Html.Partial("EditorTemplates/CommonModel", Model);} where model is my create model

and in the edit @{Html.Partial("EditorTemplates/CommonModel", Model);} where model is my edit model... how should i define the model on the partial to cater for this?

Upvotes: 0

Views: 385

Answers (1)

devstruck
devstruck

Reputation: 1507

Sounds like a perfect case for inheritance. Create a base class with everything you need for your commonly held partial in it. Have both your editor model and your create model inherit from it. Define the base class as the model for the partial view. Pass whichever child class (create or edit) model you happen to have into your Html.Partial call.

Upvotes: 1

Related Questions