2

I am writing a survey application, and I have a form that can have potentially hundreds of input fields. How would I write the controller to process this form? I can't specify each input as a parameter for the controller, that would be unrealistic.

I found out about the MVCToolkit, and the UpdateFrom() method, but is this the right way to go? If so, how would I install the MVCToolkit in my app?

2 Answers 2

6

You can accept collections as parameters in Action as well.

public ActionResult TheAction (string[] answers) {

}

Html :

<input type="text" name="answers[0]" value="" />
<input type="text" name="answers[1]" value="" />
<input type="text" name="answers[2]" value="" />
<!--and so on -->

The default model binder will automatically populate the answers array in your action with the values entered in the form.

Sign up to request clarification or add additional context in comments.

Comments

3

It is actually way simpler than that. You just need to use an object called FormCollection.

public ActionResult MyFormUpdate (int id, FormCollection form) {
    // do form updates
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.