Nopesound
Nopesound

Reputation: 510

Sharing model between view and partial view

I have a little problem. I have a view (search + search results) that I would break into two parts, I decided to create a partial view with its own controller for the form search and leave the list of results in the view. I need to know if the view and partial view can share the same instance of the model associated with the view and if so, how can I do that?

Upvotes: 3

Views: 2036

Answers (2)

Philip Pittle
Philip Pittle

Reputation: 12295

When you render your partial, you can pass it the model:

 @Html.Partial("SearchPartial", Model)

And define both your Search view and Search partial view to use the same model type. For example:

 @model SearchModel

Note: In this case the partial controller will not be called. This is by design. If you already have a model and a view, that it doesn't make sense to invoke a controller. But this mean that your page controller will need to make sure to put everything into the Model that your partial will need.

Upvotes: 2

Florian Schmidinger
Florian Schmidinger

Reputation: 4692

Html.Partial has an overload where you can pass the model. To update your result have a look at Ajax.BeginForm... Try to paste some code of what you have so far for more detailed help

Upvotes: 0

Related Questions