我在列表中显示了复选框,我想访问是否选中了哪个复选框,并想调用 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/