faruk mahir
faruk mahir

Reputation: 39

How to get checkbox row value selected in Grid.MVC

Hi I have included Grid.MVC in asp .net mvc 4 i am able to display the checkbox in grid but how do i get to know the particular checkbox is selected in a row since i want to post it to the server and check it at the controller end Below is the code any advice regarding are welcome and is there any way i can place a checkbox for header

@Html.Grid(Model).Columns(columns =>
                    {


                        columns.Add().Encoded(false).Sanitized(false).SetWidth(10).RenderValueAs(o => Html.CheckBox("checked", false));

                        columns.Add().Encoded(false).Sanitized(false).Titled("Headline").SetWidth(220).RenderValueAs(news => @Html.ActionLink(news.HeadLine, "Details", new { Id = news.Id }));

                        columns.Add(news => news.Time).Titled("News Time").SetWidth(110).Sortable(true).Filterable(true);

                    }).WithPaging(20)

Upvotes: 2

Views: 13341

Answers (3)

Kevin
Kevin

Reputation: 1

columns.Add().Titled("").Encoded(false).Sanitized(false).RenderValueAs(o => Html.CheckBox(string.Format("ArrayProperty[{0}].Selected", Model.ArrayProperty.IndexOf(o)), o.Selected));

Works for me - and a little bit cleaner than the other answer. For some reason I can't get the other item posted to work

Upvotes: 0

Rafael Delboni
Rafael Delboni

Reputation: 857

Sorry for the late reply, but today I found my self with the same question.

The following code will work as you want:

    columns.Add()
        .Encoded(false)
        .Sanitized(false)
        .SetWidth(10)
        .RenderValueAs(o => Html.CheckBox("checked", o.YourModelBoolProperty));

Good luck.

Upvotes: 3

Shivang MIttal
Shivang MIttal

Reputation: 1001

Add value attribute to the checkboxes, set their value as news.Id (if news.Id is the primary key or unique else set some value by which you will be able to identify the checked rows in controller). Add the name attribute to the checkboxes and give same name to all of them.

Html.CheckBox("checked", false, new {name = "assignChkBx"})

Use a action in controller with form colection object to get the checkbox values

 public void Controls(FormCollection form)

You will get only those values in FormCollection which are checked.

  var checkBox = form.GetValues("assignChkBx");
   if (checkBox != null)
                    {
                        foreach (var id in checkBox)
                        {
                        }
                     }

Upvotes: 1

Related Questions