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.

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

4 Comments

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..
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?
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
another option would be to compile the list using jquery. you can get checked like this stackoverflow.com/questions/1287592/… and unchecked like this stackoverflow.com/questions/8465821/… and then send the results to the controller with an ajax call
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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.