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