Ricibald
Ricibald

Reputation: 9929

ASP.NET MVC 3: Reuse the same form in all the web site

I have a Razor ASP.NET MVC 3 web site.

I have a web site with this structure for all pages: - header with search text box in top-right - body - footer

I would like to have two distinct views with their own specific model. I would like to have two distinct method: one for the search and one for the body actions.

How to organize this? If I use two partial views I have to pass SearchModel around all pages for all controllers for all methods.

How to deal with this? Thanks

Upvotes: 1

Views: 579

Answers (2)

Electrionics
Electrionics

Reputation: 6772

There are 2 ways to reuse your search form: First is to use @Html.RenderAction() at _Layout.cshtml that will render your search form to view. Second is to use @Html.RenderPartial() at _Layout.cshtml, and model will pass to view throught ViewBag object or ViewData dictionary from global action flter.

Upvotes: 1

pjumble
pjumble

Reputation: 16960

For the search box in your view you can use @Html.Action to call a child action allowing it to build the SearchModel / search view independently from the current action.

http://haacked.com/archive/2009/11/18/aspnetmvc2-render-action.aspx

Upvotes: 1

Related Questions