0

I have a Form which will dynamically add textboxes for a particular database field. I wanted to use Model Binding to retrieve values from the Form on a Controller. I know Model Binding uses the textbox's id to retrieve values. However it's never recommended to have multiple textboxes with same ID as its unique. I have it such that in case a user needs more textboxes for the same field, they can add them dynamically by clicking a button. How do I retrieve values from these textboxes? Is there a way I can use html classes with model binding to retrieve the values instead? Or should I do it like this

       public ActionResult MyAction(FormCollection form)
        {
   // ModelBinder will set "form" appropriately
      foreach(var value in form.Getvalues("duplicatedFieldId"))
         {
          //do something with value
         }
        }

Regards

Dev
  • 1,146
  • 2
  • 19
  • 30
  • Model binding does not use a controls `id` attribute, it uses a controls `name` attribute. What is the model you want to bind to (a simple array of `string` or a collection of complex objects)? –  Mar 21 '15 at 11:05
  • I want to pass an array of string items retrieved from the textboxes. Can I give the textboxes the same name then iterate through them and retrieve there values? – Dev Mar 21 '15 at 12:21
  • Give each textbox the same name attribute (say ``) and then post to `public ActionResult MyAction(string[] myName) {` (or `List myName`) then you can loop through the values. Note duplicate `id` attributes are invalid html but duplicate `name` attributes are fine. –  Mar 21 '15 at 12:26
  • If you have added the previous comment as an answer, then I would have marked it as an answer. If you do so I'll make it for an answer for users who will view this later on. – Dev Mar 21 '15 at 12:51
  • @Stephen so ideally this should work ` public ActionResult MyAction(FormCollection form) { List emailList = new List(); foreach(var value in form.GetValues("TeamLeader")) { emailList.Add(value) } }` – Dev Mar 21 '15 at 13:03
  • You don't need (and should not use) `FormCollection` If the textboxes are named `TeamLeader` then the method should be `public ActionResult MyAction(List TeamLeader)` - the collection will be bound when you post back –  Mar 21 '15 at 13:07
  • Model Binding will automatically map values Textboxes named TeamLeader into the List parameter passed. And in the case of my Form, I have multiple textboxes I need there values retrieved so the HTTPPOST action method will automatically look like this? **public ActionResult MyAction(List TeamLeader, List Implementer, List Approver)** Thanks a lot for engaging me so much – Dev Mar 21 '15 at 13:21
  • That why I asked if t was a collection of string or a collection of complex objects (which is obviously is even thought you said it was just an array of strings). This is not the way to do it for complex objects. I suggest you read my answers [here](http://stackoverflow.com/questions/29161481/post-a-form-array-without-successful/29161796#29161796) and [here](http://stackoverflow.com/questions/28019793/submit-same-partial-view-called-multiple-times-data-to-controller/28081308#28081308) for some options as to how to do this correctly –  Mar 21 '15 at 13:43
  • What do you mean by complex objects, because as I see it I have only passed 3 List objects into the constructor. In this case will they be complex objects. Thanks for the links, am going through the answers there too. – Dev Mar 21 '15 at 14:09

0 Answers0