加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

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

发布时间:2020-12-16 03:18:06 所属栏目:asp.Net 来源:网络整理
导读:我在列表中显示了复选框,我想访问哪个复选框被选中,并且想要调用控制器操作,我从下拉列表中选择选项 div id="pnlSent" style="display: none" %= Html.DropDownList("select","msgtype") % % foreach (Usp_GetUserMessagesResult w in (ListUsp_GetUserMessa
我在列表中显示了复选框,我想访问哪个复选框被选中,并且想要调用控制器操作,我从下拉列表中选择选项

<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中你应该知道你希望能够在控制器端以表格形式访问的信息,我注意到你没有在你的复选框中添加一个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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读