title: Get ID of control from View into Controller
body: <p>In my MVC application I have a view where I will display different data from a SQL table.  This will generate different amount of drop down lists and text boxes, depending on what is passed in from the Model.  </p>
  
  <p>My issue is if I want to then use that data I can't seem to figure out how I can relate control X to object Y in SQL.   For example, if I have 2 textboxes that I want to do an update on, then when the Post happens in my application the FormCollection parameter will let me see the Value of the objects, but not their control name or any form of identifying factor.  </p>
  
  <p>I could set the Value to a combination of the entered value + a name, then split this, but it seems very much like a lazy workaround.   </p>
  
  <p>I've tried to assign an ID to each, for example:</p>
  
  <pre><code>    @foreach (DataObject item in Model.AllDataObjects)
      {
          &lt;tr&gt;
              &lt;td&gt;
                  @Html.DisplayFor(modelItem =&gt; item.Name)
              &lt;/td&gt;
              &lt;td&gt;
                  @Html.DisplayFor(modelItem =&gt; item.Data)
              &lt;/td&gt;
              &lt;td&gt;
                  @if (item.Rule.Contains("Yes;No"))
                  {
                      @Html.DropDownListFor(model =&gt; item.Value, new List&lt;SelectListItem&gt;
                          {
                              new SelectListItem {Text="Yes", Value="Yes"},
                              new SelectListItem {Text="No", Value="No" }
                              }, new { @id = item.ObjectId });
                  }
                  else
                  {
                      @Html.TextAreaFor(model =&gt; item.Value, new { style = "width: 400px;", @rows = 5, @id = item.ObjectId  })
                  }
              &lt;/td&gt;
          &lt;/tr&gt;
      }
  </code></pre>
  
  <p>Edit: The following is my Post ActionResult method in the Controller, albeit <strong>it isn't complete</strong> as I can't figure out how to get an ID for the control from the FormCollection</p>
  
  <pre><code>    [HttpPost]
      [ValidateInput(false)]
      public ActionResult UpdateData(FormCollection collection, int objectId=0)
      {
          try
          {
                  int propertyTypeId = 0;
                  string propertyValue = string.Empty;
  
              // Get all the control values from the collection
              string[] allValues = new string[] { };
              IValueProvider valueProvider = collection.ToValueProvider();
              foreach(string key in collection.Keys)
              {
                  ValueProviderResult result = valueProvider.GetValue(key);
  
                  allValues = result.RawValue as string[];
  
              }
  
                  ObjectData objectData = _execution.GetObjectDetails(0);
  
                  UpdateDataResponse result = _execution.UpdateData(0, objectId,
                      objectValue, UserName);
  
                  return RedirectToAction("Details",
                      new { ObjectId = objectData.ObjectId, error = result.ErrorMessage });
          }
          catch (Exception ex)
          {
            // My exception handling here
          }
      }
  </code></pre>
  
  <p>So I can see in the mark-up that the controls are assigned the object ID as their own ID, but how can I get this back?  When I check FormCollection I only see the values for each control, but no way of identifying which is which.</p>
  
  <p>Edit: I'm using MVC version 4.  </p>