如何获取webgrid复选框值。 [英] How to fetch a webgrid checkbox values .
问题描述
控制器
公共类HomeController:控制器
{
私人readonly List< favouritegivenname> _mostPopular;
public HomeController()
{
_mostPopular = new List< favouritegivenname>()
{
new FavouriteGivenName(){Id = 1,Name =Jack,Age = 30},
new FavouriteGivenName(){Id = 2,Name =Riley,年龄= 40},
new FavouriteGivenName(){Id = 3,Name =William,Age = 17},
new FavouriteGivenName(){Id = 4, Name =Oliver,Age = 56},
new FavouriteGivenName(){Id = 5,Name =Lachlan,Age = 25},
new FavouriteGivenName( ){Id = 6,Name =Thomas,Age = 75},
new FavouriteGivenName(){Id = 7,Name =Joshua,Age = 93},
new FavouriteGivenName(){Id = 8,Name =James,Age = 15},
new FavouriteGivenName(){Id = 9,Name =Liam,Age = 73} ,
new FavouriteGivenName(){Id = 10,Name =Max,Age = 63}
};
}
public ActionResult Index(int?页面)
{
返回查看(_mostPopular.ToList());
}
[HttpPost]
公共ActionResult分配(FormCollection表格)
{
//获取所选ID的集合
var chckedValues = form.GetValues(assignChkBx);
//你可以使用每个对象,如果你希望来自id
foreach(var idValues中的var id)
{
//获取对象示例..客户Customer = Customers.find(id);
//这里的方法就像向顾客发送物品
}
返回RedirectToAction(index );
}
}
型号
Controller
public class HomeController : Controller
{
private readonly List<favouritegivenname> _mostPopular;
public HomeController()
{
_mostPopular = new List<favouritegivenname>()
{
new FavouriteGivenName() {Id = 1, Name = "Jack", Age = 30},
new FavouriteGivenName() {Id = 2, Name = "Riley", Age = 40},
new FavouriteGivenName() {Id = 3, Name = "William", Age = 17},
new FavouriteGivenName() {Id = 4, Name = "Oliver", Age = 56},
new FavouriteGivenName() {Id = 5, Name = "Lachlan", Age = 25},
new FavouriteGivenName() {Id = 6, Name = "Thomas", Age = 75},
new FavouriteGivenName() {Id = 7, Name = "Joshua", Age = 93},
new FavouriteGivenName() {Id = 8, Name = "James", Age = 15},
new FavouriteGivenName() {Id = 9, Name = "Liam", Age = 73},
new FavouriteGivenName() {Id = 10, Name = "Max", Age = 63}
};
}
public ActionResult Index(int? page)
{
return View(_mostPopular.ToList());
}
[HttpPost]
public ActionResult Assign(FormCollection form)
{
//get collection of selected ids
var chckedValues = form.GetValues("assignChkBx");
//You can use each object if u wish from id
foreach (var id in chckedValues)
{
//get object example.. Customer Customer = Customers.find(id);
//Your Method here like send an item to customer
}
return RedirectToAction("index");
}
}
Model
public class FavouriteGivenName
{
public int Id { get; set; }
public string Name { get; set; }
public int Age { get; set; }
}
查看
@model列表< mvcapplication1.models.favouritegivenname>
<!DOCTYPE html>
< html>
< head>
< title> Index< / title>
< style type =text / css>
.webGrid {margin:4px;边界崩溃:崩溃;宽度:300px; }
.header {background-color:#E8E8E8; font-weight:bold;颜色:#FFF; }
.webGrid th,.webGrid td {border:1px solid#C0C0C0;填充:5px; }
.alt {background-color:#E8E8E8;颜色:#000; }
。人员{宽度:200px; font-weight:bold;}
< / style>
< / head>
< body>
@using(Html.BeginForm(Assign,Home))
{
var grid = new WebGrid(Model,canPage :true,rowsPerPage:2);
grid.Pager(WebGridPagerModes.NextPrevious);
@ grid.GetHtml(tableStyle:webGrid,
htmlAttributes:new {id =DataTable},
headerStyle:header,
alternatingRowStyle:alt,
列:grid.Columns(
grid.Column(Name),
grid.Column(标题:分配?,格式:@< text>< input class =check-boxid =assignChkBxname =assignChkBxtype =checkboxvalue =@ item.Id/>)// grid.Column(null,format:@< text> < input name =HasFormgivarentype =checkboxvalue =@ item.ID/>)
));
}
< / body>
< / html>
但是当复选框状态改变时,控制器中的Assign方法没有触发
View
@model List<mvcapplication1.models.favouritegivenname>
<!DOCTYPE html>
<html>
<head>
<title>Index</title>
<style type="text/css">
.webGrid { margin: 4px; border-collapse: collapse; width: 300px; }
.header { background-color: #E8E8E8; font-weight: bold; color: #FFF; }
.webGrid th, .webGrid td { border: 1px solid #C0C0C0; padding: 5px; }
.alt { background-color: #E8E8E8; color: #000; }
.person { width: 200px; font-weight:bold;}
</style>
</head>
<body>
@using (Html.BeginForm("Assign","Home"))
{
var grid = new WebGrid(Model, canPage: true, rowsPerPage: 2);
grid.Pager(WebGridPagerModes.NextPrevious);
@grid.GetHtml(tableStyle: "webGrid",
htmlAttributes: new { id = "DataTable" },
headerStyle: "header",
alternatingRowStyle: "alt",
columns: grid.Columns(
grid.Column("Name"),
grid.Column(header: "Assign?", format: @<text><input class="check-box" id="assignChkBx"name="assignChkBx" type="checkbox" value="@item.Id"/>) // grid.Column(null, format: @<text><input name="HasFormgivaren" type="checkbox" value="@item.ID" />)
));
}
</body>
</html>
But Assign method in controller is not firing when checkbox state changes
推荐答案
如果在检查或取消选中复选框时需要执行Assign()添加以下脚本 -
If you need to execute "Assign()" while checking or un-checking the check boxes then add following script-
function submitForm(currControl) {
(currControl).closest(' form')。submit();
}
(currControl).closest('form').submit(); }
然后将格式更改为 -
格式:@
如果你需要其他任何东西,请告诉我。
then change the format to-
format: @
If anything other you need then please let me know.
这篇关于如何获取webgrid复选框值。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!