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

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

我在列表中显示了复选框,我想访问是否选中了哪个复选框,并想调用 Controller 操作,我从下拉列表中选择选项

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

最佳答案

在 asp.net-MVC 中,您应该以表单形式在 Controller 端访问您希望能够访问的信息,我注意到您没有在复选框中添加 ID,我的是动态绑定(bind)的,我正在使用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"/>
 <% } %>

然后在 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 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
  }

该站点提供了有关在 View 中处理控件时如何检查 Controller 信息的更多详细信息:
http://quickstarts.asp.net/previews/mvc/mvc_HowToRenderFormUsingHtmlHelpers.htm

关于.net - 在 Mvc asp.net 中获取选中的复选框值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/468742/

相关文章:

C# 将希伯来历日期转换为公历日期

c# - 如何连接到 RootDSE 和/或使用 System.DirectoryServices.Protocols 检索 highestCommittedUSN?

c# - jQuery 是在 C#.net 环境中处理用户交互的最佳方式吗?

javascript - 在剑道网格的列中调用JS函数

.net - 一个空的委托(delegate)会吃掉内存吗?

c# - 从 web.config 获取连接字符串以在 EF POCO 中使用?

javascript - 使用 Javascript 在标签上倒计时 30 秒

c# - 如何在 ASP.NET WebAPI 中返回文件 (FileContentResult)

asp.net-mvc - TFS 上的 Visual Studio 2013 构建错误

c# - 如何解决此代码重复+添加另一种方法