Click here to Skip to main content
15,998,008 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
After Paging how to hold checkbox values in mvc 4 using web grid
Posted
Comments
Store in Session.
sai sagar 11-Oct-13 5:24am    
how is it possible. can you explain brefily

1 solution

Hi i done my self.. please refer below code.

in controller
------------------------------------
public ActionResult Index()
{
var players = new List<player>
{
new Player {Id = "101", Rank = 1, Name = "Sai", Country = "NOR", Points = 16},
new Player {Id = "102", Rank = 2, Name = "sagar", Country = "RUS", Points = 14},
new Player {Id = "103", Rank = 3, Name = "sai sagar", Country = "SWE", Points = 13},
new Player {Id = "104", Rank = 4, Name = "yash", Country = "USA", Points = 12},
new Player {Id = "105", Rank = 5, Name = "Anil", Country = "SWE", Points = 11},
new Player {Id = "106", Rank = 6, Name = "Ravi", Country = "NOR", Points = 11},
new Player {Id = "107", Rank = 7, Name = "pradep", Country = "CAN", Points = 11},
new Player {Id = "108", Rank = 8, Name = "sai", Country = "FIN", Points = 9},
new Player {Id = "109", Rank = 8, Name = "sagar", Country = "CAN", Points = 9},
new Player {Id = "110", Rank = 10,Name = "Paul", Country = "USA", Points = 9}
};

ViewData["SelectList"] = HttpContext.Session["SelectList"] ?? new List<string>();

return View(players);
}

[HttpPost]
public ActionResult Select(bool isChecked, String id)
{
var selectList = (List<string>)HttpContext.Session["SelectList"] ?? new List<string>();
if (isChecked && !selectList.Contains(id))
{
selectList.Add(id);
}
else if (!isChecked && selectList.Contains(id))
{
selectList.RemoveAll(s => s == id);
}
HttpContext.Session["SelectList"] = selectList;

return Content("OK");
}

----------------------------

in view


@model IEnumerable your modelname
<script src="~/Scripts/jquery-1.7.1.js"></script>
<script src="~/Scripts/jquery-ui-1.8.20.js"></script>
<script src="~/Scripts/knockout-2.1.0.js"></script>

<script type="text/javascript">
$(function () {

$(':checkbox').change(function () {
$.ajax({
url: '@Url.Action("Select")',
type: 'POST',
data: { isChecked: $(this).is(':checked'), id: $(this).val() },
success: function (result) {
}
});
});
});
</script>

@{
var grid = new WebGrid(source: Model,
defaultSort: "Rank",
canSort: true,
rowsPerPage: 5
);
}



@grid.GetHtml(
tableStyle: "grid",
headerStyle: "head",
alternatingRowStyle: "alt",
columns: grid.Columns(
grid.Column("Select", format: @<text><input name="Id" type="checkbox" value="@item.Id" @Html.Raw(((List<string>)ViewData["SelectList"]).Contains(@item.Id) ? "checked" : "") />),
grid.Column("Rank", "Rank"),
grid.Column("Name", "Name"),
grid.Column("Country", "Country"),
grid.Column("Points", "Points")
)
)
C++






in model

C#
public class Player
    {
        public String Id { get; set; }
        public int Rank { get; set; }
        public String Name { get; set; }
        public String Country { get; set; }
        public int Points { get; set; }
    }
 
Share this answer
 

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