我更新了代码,这是问题的核心解决方案
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Edit(int[] rb, int id)
{
List<nastava_prisustvo> nastava = new List<nastava_prisustvo>();
string poruka = "";
for (int i = 1; i <=rb.Length; i++)
{
string name = "chk" + i;
string selID = Request.Form[name];
if (selID == "on")
{
//poruka = poruka + "Polje sa rednim brojem "+ i +" je chekirano\n";
nastava.Add(new nastava_prisustvo
{
br_indexa = int.Parse(Request.Form["id_stud"+i]),
id_predmet = id
});
}
}
// ViewData["Message"] = poruka;
return View("show", nastava);
}
查看:
<table class="data-table">
<tr>
<th>
Redni br.
</th>
<th>
Br. Indexa
</th>
<th>
Prezime
</th>
<th>
Ime
</th>
<th>
<input id="check_all" type="checkbox" onclick="function" />
</th>
</tr>
<% int rb = 1;%>
<% foreach (var item in Model)
{ %>
<tr>
<td>
<input readonly="readonly" class="input-box" id="rb" type="text" name="rb" value="<%= Html.Encode(rb)%>" />
</td>
<td>
<input readonly="readonly" class="input-box" id="id_stud" type="text" name="id_stud" value="<%= Html.Encode(item.id_stud)%>" />
</td>
<td>
<%= Html.Encode(item.prezime)%>
</td>
<td>
<%= Html.Encode(item.ime)%>
</td>
<td>
<input id="check" name="chk<%= Html.Encode(rb)%>" type="checkbox" />
</td>
</tr>
<% rb = rb + 1;%>
<% } %>
</table>
答案 0 :(得分:0)
这是一个演示如何将checkbox的值传递给控制器的示例。
查看:
<%@ Page Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage" %>
<asp:Content ID="indexTitle" ContentPlaceHolderID="TitleContent" runat="server">
Home Page
</asp:Content>
<asp:Content ID="indexContent" ContentPlaceHolderID="MainContent" runat="server">
<h2><%= Html.Encode(ViewData["Message"]) %></h2>
<p>
To learn more about ASP.NET MVC visit <a href="http://asp.net/mvc" title="ASP.NET MVC Website">http://asp.net/mvc</a>.
<form action="/home/index" method="post">
<input type="checkbox" name="cb" value="value1" />
<input type="checkbox" name="cb" value="value2" />
<input type="submit" />
</form>
</p>
</asp:Content>
控制器:
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Index(string cb)
{
ViewData["Message"] = cb;
return View();
}
此示例演示如何通过数组接收。
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Index(string[] cb)
{
string result = "";
foreach (string value in cb)
result += value.ToString() + " ";
ViewData["Message"] = result;
return View();
}
<form action="/home/index" method="post">
<input type="checkbox" name="cb" value="v1" />
<input type="checkbox" name="cb" value="v2" />
<input type="checkbox" name="cb" value="v3" />
<input type="checkbox" name="cb" value="v4" />
<input type="submit" />
</form>
答案 1 :(得分:0)
如果您有一个复选框列表,并且想要将已选中的复选框传递给控制器,您只需执行以下操作:
视图“项目”中每个复选框的名称
控制器动作:
public ActionResult(UpdateObject updateobject)
{
string[] list = updateobject.items;
}
更新对象定义:
public class UpdateObject
{
public string[] items { get; set; }
}