简体   繁体   English

在Mvc asp.net中获取选中的复选框值

[英]getting selected check box value in Mvc asp.net

I had displayed check box in a list and i want to access that which check box is selected or not and want to call controller action where i select option from dropdown 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>

In asp.net-MVC you should ahve the info that you want to be able to access on the controller side in a form as so, I noticed you did not add an ID to your checkbox, mine is dynamically binded and I am using the HTML helper that comes with asp.net-mvc: 在asp.net-MVC中你应该知道你希望能够在控制器端以表格形式访问的信息,我注意到你没有在你的复选框中添加一个ID,我的动态绑定我正在使用asp.net-mvc附带的HTML帮助:

   <% 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 could access the information like this: 然后在控制器上你可以访问这样的信息:

   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
  }

This site gives more details on how to check information on the controller as controls are handled in the view: http://quickstarts.asp.net/previews/mvc/mvc_HowToRenderFormUsingHtmlHelpers.htm 该站点提供了有关如何在视图中处理控件时检查控制器上的信息的更多详细信息: http//quickstarts.asp.net/previews/mvc/mvc_HowToRenderFormUsingHtmlHelpers.htm

If you're planning to submit this form, and do anything with the checkbox values on the server side, you'll want to give them name and value attributes (and the name should be the same for each checkbox in the group). 如果您计划提交此表单,并对服务器端的复选框值执行任何操作,则需要为其指定名称属性(并且组中的每个复选框的名称应相同)。 The checkbox helper that TStamper mentioned takes care of that for you. TStamper提到的复选框帮助程序会为您解决这个问题。

If you just want a client action to happen when the checkbox is ticked or unticked, you can do something like this (I'm assuming that these objects have some kind of key field; I'm calling it MessageID ): 如果您只是想勾选或取消勾选复选框时发生客户端操作,您可以执行以下操作(我假设这些对象具有某种键字段;我称之为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)"  />

Please note that id values are required to be unique within an HTML document. 请注意, id值必须在HTML文档中是唯一的。 So creating lots of checkboxes with the ID "Checkbox1" is not a good idea. 因此,使用ID“Checkbox1”创建大量复选框并不是一个好主意。 (If that input element were runat="server", then .NET would generate a unique HTML id from the WebForms ID . (如果该输入元素是runat =“server”,那么.NET将从WebForms ID生成唯一的HTML id

You can loop through all of the controls on the div pnlsent and if the control type is a checkbox you can then determine if the checkbox is checked. 您可以遍历div pnlsent上的所有控件,如果控件类型是复选框,则可以确定是否选中了复选框。

Example of looping through controls in VB.... 在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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM