Getting selected checkbox value in asp.net mvc

I have displayed a checkbox in the list, and I want to access which checkbox is checked or not, and you want to trigger the controller action when I select an option from the drop-down list

<div id="pnlSent" style="display: none">
            <%= Html.DropDownList("select","msgtype") %>
            <% foreach (Usp_GetUserMessagesResult w in (List<Usp_GetUserMessagesResult>)ViewData["UListM"])
               { %>
                <li>
            <div class="question-info">

                <input id="Checkbox1" type="checkbox" onclick="function() { alert("df") ; } "  />

                <div class="views count"><span></span></div>
                <div class="question">
                    <div class="meta">Subject:  <%= w.Subject %></div>
                    <div class="meta">To: <%= w.Username  %></div>
                    <h3 class="title"><%= Html.Encode(w.Mesg)%></h3>
                </div>
            </div>
        </li>
            <% } %>

    </div>
+5
source share
3 answers

In asp.net-MVC, you should get the information that you want to have access to on the controller side of the form, since I noticed that you did not add the identifier to your checkbox, my is dynamically linked, and I am using the HTML helper that comes with asp.net-mvc:

   <% 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>      

       </tr>  
    <%} %>
   <input type"submit"/>
 <% } %>

then on the controller you can access the following information:

   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 checkbox=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
  }

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

+2

- , ( ). , TStamper, .

, , , - ( , - , MessageID):

<script type="text/javascript">
function handleCheckbox( theBox ) {
  if( theBox.checked) {
    // do checked stuff
  }
  else {
    // do un-checked stuff
  }
}
</script>

...

<input type="checkbox" value="<%= w.MessageID %>" onclick="handleCheckbox(this)"  />

, id HTML. "Checkbox1" - . ( runat = "server", .NET HTML id WebForms ID.

+1

div pnlsent, , , .

VB....

 For Each ctrl As Control In Page.Controls
        If TypeOf ctrl Is TextBox Then
            CType(ctrl, TextBox).BackColor = clr
        Else
            If ctrl.Controls.Count > 0 Then
                SetTextBoxBackColor(ctrl, clr)
            End If
        End If
    Next
-1

All Articles