views:

61

answers:

1

I'm using ASP.NET MVC, and I have a widget within my form collection on my HTML page, and when I click on the save button for the main form I would like not only to save the data on the main form but also the content on the widget, how can I do this?

+1  A: 

If the widget generates its own form, then you are out of luck, as the view would render two nested forms, which will not work:

This will now work:
<form>
  <your input>
  <widget>
    <form>
      <widget's input>
    </form>
</form>

If your widget does not that, then you open your form in your view, add your inputs, include the widget, add the submit button, and close the form.

Then you have two options.

  1. You create an action that accepts the Post verb, and as parameters accepts a FormsCollection. This is a dictionary which will contain all your inputs, both those written by you, and those by the widget. You will then manage the post as you prefer.

  2. You create a model object with the names of all your inputs, make the view stringly typed, then accept that model as a parameter to your action. This has the advantage that all will be more transparent (as the data binder will do much of the work) but it could be a bit trickier to set up

Palantir