2

I am new to MVC 4. I am stuck in a situation and want some suggestions to resolve the problem. The problem scenario is:

I am rendering a WebGrid inside a partial view and the WebGrid format is as follows: enter image description here

An IEnumerable collection is bound with the WebGrid. The view for binding WebGrid is:

@{    
MIS.Areas.AdminModule.Models.AdminModuleViewModels.Module_UserGrp_Permission allPermissions = new MIS.Areas.AdminModule.Models.AdminModuleViewModels.Module_UserGrp_Permission();    

}

    @{
var grid = new WebGrid(Model, canPage: true, rowsPerPage: 10, selectionFieldName: "selectedRow", ajaxUpdateContainerId: "Title");
grid.Pager(WebGridPagerModes.NextPrevious);}
    <div id="gridContent">
        @grid.GetHtml(tableStyle: "webGrid",
            headerStyle: "header",
            alternatingRowStyle: "alt",
            selectedRowStyle: "select",
            columns: grid.Columns(
            grid.Column(header: "Select",
            format: @<input class="select" id="assignChkBx" name="assignChkBx" type="checkbox" @allPermissions.intMenuId/>),
            grid.Column(header: "MenuId", format: (item) => item.intMenuId, style: "description"),
            grid.Column(header: "Menu", format: (item) => item.strMenuName, style: "description", canSort: true),
            grid.Column(header: "Add", format: @<text><input name="Add" type="checkbox"  @(item.boolAddPer == true ? "Checked" : null) id="chkboxIsActiveAdd" /></text>),
            grid.Column(header: "Edit", format: @<text><input name="Edit" type="checkbox"  @(item.boolEditPer == true ? "Checked" : null) id="chkboxIsActiveEdit" /></text>),
            grid.Column(header: "Delete", format: @<text><input name="Delete" type="checkbox"  @(item.boolDeletePer == true ? "Checked" : null) id="chkboxIsActiveDelete" /></text>),
            grid.Column(header: "Grant", format: @<text><input name="Grant" type="checkbox"  @(item.boolGrantPer == true ? "Checked" : null) id="chkboxIsActiveGrant" /></text>)

     ))                       
    </div>

And fetching data from database as follows (I am NOT using EntityFramework) :

var result = from column in dt.AsEnumerable()

                     select new Module_UserGrp_Permission
                     {
                         intMenuId = Convert.ToInt32(column["MenuId"]),
                         intUserGrpId = Convert.ToInt32(column["UserGrpId"]),
                         strMenuName = Convert.ToString(column["MenuName"]),

                         boolAddPer = Convert.ToBoolean(column["boolGAdd"]),
                         boolEditPer = Convert.ToBoolean(column["boolGEdit"]),
                         boolDeletePer = Convert.ToBoolean(column["boolGDel"]),
                         boolViewPer = Convert.ToBoolean(column["boolGView"]),
                         boolGrantPer = Convert.ToBoolean(column["boolGGrant"])
                     };
        return new List<MIS.Areas.AdminModule.Models.AdminModuleViewModels.Module_UserGrp_Permission>(result);

Now the problem is I have to save all the checked/unchecked items from this WebGrid. What should I do to save all the values after clicking the 'Save' button. Please suggest possible solutions.

Thank you all.

BAdmin
  • 927
  • 1
  • 11
  • 19

2 Answers2

1

Since all of your check boxes have the same name you can do a

string result = Request.Form["assignChkBx"].ToString();

on your controller which will give you a list of all of the checked checkboxes. Hopefully this helps.

Matt Bodily
  • 6,403
  • 4
  • 29
  • 48
  • Thank you for your reply sir. I'll definitely give it a try. But you know I have to get all the checked/unchecked values from all the Add-Edit-Delete-Grant columns (the first select checkbox is not important as I have to take all the values). Also edited the checkbox names. I'll get back to you. Thanks.. – BAdmin Nov 05 '13 at 06:10
  • Sir, following your suggestion I am able to get the values in 'on' format for checked items. But the problem is the values are not coming row wise, more over I can't get the unchecked values. So, how would I update all the values? – BAdmin Nov 05 '13 at 11:35
  • this is the easiest way to do it. If you give each column its own name you can get a list of what is checked by doing a request on each name. Since there are multiple on a row I would just query to get a list of available and compare with what is checked to get the unchecked – Matt Bodily Nov 05 '13 at 14:09
  • another option would be to compile the list using jquery. you can get checked like this http://stackoverflow.com/questions/1287592/get-checkbox-list-values-with-jquery and unchecked like this http://stackoverflow.com/questions/8465821/find-all-unchecked-checkbox-in-jquery and then send the results to the controller with an ajax call – Matt Bodily Nov 05 '13 at 14:10
0

While it's not a coded worked solution, I've managed to get this working with a single checkbox. Should be simple enough to get it wired up for multiple: ASP.NET MVC Display an HTML Table with Checkboxes to Select Row Items

Tim Meers
  • 928
  • 1
  • 14
  • 24