tags:

views:

278

answers:

2

I have a number of dynamically created fields on the views. Lets say

Car Payment [1] [2] [3]
House Payment [1] [2] [3]
CC Payment [1] [2] [3]

In my controller I want to see what values the user has selected for each one but I find that using the FormCollection I only get back "Car Payment", "House Payment", and "CC Payment". I do not get back the actual values associated with those radiobutton selections unless I explicitly define those inputs as a controller variable. Unfortunately these radio buttons are dynamically generated, including their names so this isn't an option. Any ideas in terms of handling this problem?

A: 

Ok I figured out how to do this in the controller, thought I'd share.

Request["CarPayment"] will be able to dynamically extract values from the RadioButton collection in my example.

Al Katawazi
+1  A: 

I think you really ought to be using the ValueProvider on the controller rather than referencing the parameters in the request itself. Using the value provider will make it much easier to unit test since you won't have to mock up the request, just provide a ValueProvider implementation to the controller.

  ...
  var provider = new FormCollection();
  provider["CarPayment"] = "1";

  var controller = new PaymentController();
  controller.ValueProvider = provider.ToValueProvider();

  var result = controller.Create();
  ...


  public ActionResult Create()
  {
      int carPaymentType = (int)this.ValueProvider["CarPayment"].ConvertTo(typeof(int));
      ...
  }

Obviously, you'll need to add error checking, etc. You can also iterate through the keys on the ValueProvider just like you would on Request.Form.

tvanfosson