Calculating form column values ​​from table in ASP.NET MVC?

Ok, I'm an MVC newbie coming from a webforms background so please excuse me please any ignorance. Here's my scenario. I have a table that consists of a list of apps and their associated permissions. Each row in the table has three pieces of information: a checkbox, text describing the row, and a drop-down list that allows the user to select the appropriate permission for the application. I want to post this data and only work with rows in the table that have been checked (row id is embedded as 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 page 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 do I get the corresponding values ​​from the FormCollection when I get to the controller in the form post? I've done this in the past when I only had checkbox values ​​to retrieve by simply calling Request.Form ["CheckBoxName"] and parse the string.

Or am I completely wrong about this?

+1


source to share


2 answers


You are halfway through to post your data that the controller can read the information that should be inside the form:

       <% 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 provides more details on how to handle the dropdownlist helper:

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

+2


source


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

So one way would be to give the checkboxes different names, eg. "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 specific structured name, and the binder by default will wrap each set of form elements in a list of typed records in the controller. This is fully explained in this blog post .

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

+1


source







All Articles