Click here to Skip to main content
15,892,005 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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


C#
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
Posted
Updated 5-Nov-13 22:03pm
v2

1 solution

If you need to execute "Assign()" while checking or un-checking the check boxes then add following script-
C#
function submitForm(currControl) {
            $(currControl).closest('form').submit();
        }


then change the format to-

format: @

If anything other you need then please let me know.
 
Share this answer
 
Comments
yeshgowda 7-Nov-13 5:52am    
hi pradipta,
i used the code as u suggested but give a "Microsoft JScript runtime error: Object expected" at $(currControl) line
here is my view
@model List<mvcapplication1.models.favouritegivenname>

<!DOCTYPE html>

<html>
<head>
<title>Index</title>
<script language="javascript">
function submitForm(currControl) {
$(currControl).closest('form').submit();
}
</script>
<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: @<input class="check-box" id="assignChkBx" name="assignChkBx" type="checkbox" value="@item.Id" önclick="submitForm(this)"/>)

));
}
</body>
</html>

what mistake i done ?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900