1

I have a model with a property that points to a file that contains HTML. My strongly typed view to this model uses a custom HTML helper method to resolve and return the HTML from the file. Works great so far.

The HTML read from each file will contain various controls whose values I need to retrieve when the form is POSTed.

What would be the best way to have access to the POSTed control values in my controller method?

I would prefer a non jQuery solution, but I am not sure if the MVC framework can provide these values to me? Can it provide a list of key/value pairs to the controller somehow?

Lorenzo
  • 29,081
  • 49
  • 125
  • 222
John Livermore
  • 30,235
  • 44
  • 126
  • 216

3 Answers3

3

You could use the FormCollection in ASP.NET MVC.

public ActionResult SomeAction(FormCollection form) {
    ...
}
dotariel
  • 1,584
  • 12
  • 23
2

You have essentially two options.

1) Use the old fashioned Request variables as all we have done in ASP.NET web forms.

For example in your controller action method you can retrieve any value present on the form with the following method

public ActionResult SomeAction() {
    var request = this.ControllerContext.HttpContext.Request;
    bool boolParam = bool.Parse( request["boolParam"] ?? "false" );
}

2) Create a custom Model Binder to let the framework pack those values in a custom class object.

This method would be a little bit more difficult at the beginning because you have to create a custom Model Binder but it favour readability on your controller code. For further details on creating custom model binders give a look at the following links (you can find more with a simple search)

Hope it helps

Community
  • 1
  • 1
Lorenzo
  • 29,081
  • 49
  • 125
  • 222
  • You should definitely go the ModelBinder route - much more maintainable and it is the spot to do this in MVC. You are basically going through the form, but at least it's in one spot. – Keith Rousseau Nov 01 '10 at 13:17
0

Is the content of the HTML files dynamic or known at design time? If you know it now, you could have each one post to it's own action and then strongly type the parameters.

Ryan
  • 4,303
  • 3
  • 24
  • 24