tags:

views:

663

answers:

2

Ok, I'm an MVC newbie coming from a webforms background, so please excuse any ignorance here. Here is my scenario. I've got a table consisting of a list of applications and associated permissions. Each table row consists of 3 pieces of information: a checkbox, some text describing the row, and a dropdown list allowing the user to select the appropriate permission for the application. I want to post this data and only work with the rows in the table which were checked (the id of the row is embedded as the checkbox name). From there, I want to grab the selected value from the DropDownList, and call the necessary code to update the DB. Here is my View page's code:

            <%foreach (var app in newApps)
              { %>
                <tr>
                    <td><input type="checkbox" name="AddApps" value="<%=app.ApplicationId %>" /></td>
                    <td><%=Html.Encode(app.ApplicationName)%></td>
                    <td><%=Html.DropDownList("AppRole", new SelectList(app.Roles, "RoleId", "RoleDescription"))%></td>
                </tr>
              <%} %>

How would I retrieve the appropriate values from the FormCollection when I get to the controller on form post? I have done this in the past when I only had checkbox values to retrieve by just calling Request.Form["CheckBoxName"] and parsing the string.

Or am I going about this entirely wrong?

+1  A: 

You are halfway right in order to post your data that the controller can read the info it must be inside a form as so :

       <% using(Html.BeginForm("Retrieve", "Home")) %>//Retrieve is the name of the action while Home is the name of the controller
       <% { %>
            <%foreach (var app in newApps)              { %>  
          <tr> 
               <td><%=Html.CheckBox(""+app.ApplicationId )%></td>      
          <td><%=Html.Encode(app.ApplicationName)%></td>
                <td><%=Html.DropDownList("AppRole", new SelectList(app.Roles, "RoleId", "RoleDescription"))%></td> 
           </tr>  
        <%} %>
       <input type"submit"/>
     <% } %>

and on your controller :

      public ActionResult Retrieve()
    {
        //since all variables are dynamically bound you must load your DB into strings in a for loop as so:
      List<app>=newApps;
      for(int i=0; i<app.Count;i++)
      {


        var checkobx=Request.Form[""+app[i].ApplicationId];
        // the reason you check for false because the Html checkbox helper does some kind of freaky thing for value true: it makes the string read "true, false"
          if(checkbox!="false")
          {
          //etc...almost same for other parameters you want that are in thr form
          }

       }
    //of course return your view
    return View("Index");//this vaires by the name of your view ex: if Index.aspx
   }

This site gives more details on how to handle the dropdownlist helper:

http://quickstarts.asp.net/previews/mvc/mvc_HowToRenderFormUsingHtmlHelpers.htm

TStamper
+1  A: 

Request.Form will still work, except that your checkboxes all have the same name.

So one way would be to give the checkboxes distinct names, e.g. "AddApps-app.id", and use Request.Form.

However, a more elegant and testable way is to use list binding.

In this model, you give your form elements a certain structured name, and the default model binder will wrap each set of form elements into a list of typed records in the controller. This is fully explained in this blog post.

The advantage here is that your controller deals only with instances of the application type, and hence has no implicit dependency on the way the view is structured. Therefore, it is very easy to unit test.

Craig Stuntz