【问题标题】:getting selected check box value in Mvc asp.net在 Mvc asp.net 中获取选中的复选框值
【发布时间】:2009-01-22 11:24:09
【问题描述】:

我已经在列表中显示了复选框,我想访问是否选中了哪个复选框,并希望在我从下拉列表中选择选项时调用控制器操作

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

【问题讨论】:

    标签: .net asp.net asp.net-mvc


    【解决方案1】:

    在 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"/>
     <% } %>
    

    然后在控制器上你可以访问这样的信息:

       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】:

      如果您打算提交此表单,并对服务器端的复选框值执行任何操作,您需要为它们提供 namevalue 属性(并且组中每个复选框的名称应该相同)。 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 文档中必须是唯一的。因此,创建大量 ID 为“Checkbox1”的复选框并不是一个好主意。 (如果该输入元素是 runat="server",那么 .NET 将从 WebForms ID 生成唯一的 HTML id

      【讨论】:

        【解决方案3】:

        您可以循环遍历 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
        

        【讨论】:

          猜你喜欢
          • 2020-07-07
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2014-08-31
          • 2011-03-02
          • 2011-01-05
          • 2017-02-08
          • 1970-01-01
          相关资源
          最近更新 更多