1

I dynamically generate the DropDownList for each item in List of string I pass into my view, and then generate a dropdownlist for some standard options with the dropdownlist name being the letters "drp" + the String item that was passed in view via viewdata. The problem I am running into is that I can't figure out how to access the individual Dropdown List in the HttpPost in the view because the names and number of items vary.

Here is my Get Code for my View :

public ActionResult ModMapping()
        {

            ViewData["mods"] = TempData["mods"];
            return View();
        }

Here is my View Generation:

<% using (Html.BeginForm()) { %>

<h2>Modifier Needing Mapping</h2>
<p>Please Choose for each modifier listed below what type of fee it is.  There is an ingore option if it is not a gloabl fee modifier, professional fee modifier, or technical fee modifier.</p>
<table>
    <tr>
        <th>Modifier</th>
        <th>Mapping Options</th>
    </tr>
        <% int i; 
           i=0;
           var modsList = ViewData["mods"] as List<String>;%>

        <% foreach (String item in modsList) { %>
            <% i++; %>
            <tr>
                <td>
                    <%: Html.Label("lbl" + item, item) %>
                </td>
                <td>
                    <%: Html.DropDownList("drp" + item, new SelectList(
                  new List<Object>{ 
                       new { value = "NotSelected" , text = "<-- Select Modifier Type -->"},
                       new { value = "Global" , text = "Global Fee" },
                       new { value = "Professional" , text = "Professional Fee"},
                       new { value = "Technical", text = "Technical Fee"},
                       new { value = "Ingore", text="Ingore This Modifier"}
                    },
                  "value",
                  "text",
                  "NotSelected")) %>
                </td>
            </tr>
        <% } %>
</table>
 <input type="submit" value="Done" />
<% } %></code>

1 Answers1

0

This is extremely simple if you use a model for your view, as MVC will map all the dropdown lists back to your model automatically.

If you really don't want to use a model, you can access the values in the form like this:

// Load your modsList here.
foreach (String item in modsList)
{
   var dropDownValue = Request["drp" + item];
}

Another option is to write a controller function that accepts a FormCollection, which is just a simple dictionary of all the values in the POST:

[HttpPost]
public ActionResult ModMapping(FormCollection formCollection)
{
    // Load your modsList here.
    foreach (String item in modsList)
    {
       var dropDownValue = formCollection["drp" + item];
    }
}

You could probably simplify things and just loop through the FormCollection looking for items that start with "drp", depending upon what your page and requirements look like.

Community
  • 1
  • 1
Jason Berkan
  • 8,734
  • 7
  • 29
  • 39
  • Agreed you are making this extremely difficult on yourself. Using a ViewBag and an @DropDownFor() in your view would be sooooo much easier. – PW Kad Apr 09 '13 at 18:44
  • This doesn't work. What should my [HttpPost] method have as input parameters? – Stacie Bartley Apr 12 '13 at 13:58
  • @StacieBartley - Doesn't work, in what way? Through the Request object, you have access to all the data that is POSTed back to the server. I will also update the answer with an example using a FormCollection. – Jason Berkan Apr 12 '13 at 15:41