views:

58

answers:

2

I'm looking for a best practice for embedding a form on multiple pages as a partial view.

I have a contact form I'm looking to embed on multiple pages on a site. Usually, the form would be on a contact page and the contact model could be the model for the view and use data annotations for validation. However, the view is already strongly typed.

How can I create a reusable form in a partial view and embed it on the page? I'm using N2 on the site, so the pages have to already have a strongly-typed model, but I would be open to extending those objects.

+3  A: 

Personally, I recommend using for Html.RenderAction() for cross-cutting concerns such as these.

The handler for your contact form is going to need to exist independently of the page your are currently viewing so you are left with 3 options:

  1. Manually add it to the response of the current action
  2. Manually add it to the response of the current controller by way of a base controller that modifies the ViewState or ViewModel
  3. Call the RenderAction() HtmlHelper inside of the current view

Of these 3 options, while the third is technically more costly than 1 and 2 (because it initiates a brand new request), it is also the most maintanaible solution. By calling RenderAction() you have the advantage of being able to completely isolate your contact form from the rest of the view and thus you won't have to worry about hacking it into the current controller responses.

Nathan Taylor
A: 

Use RenderPartial if data model for partial view is already in main view's model, in other case use RenderAction (then the action of the partial view will create its view model itself).

Tiendq