mercredi 20 avril 2016

Checkbox click in a webgrid(edit/display mode) mvc and pass its value to controller

I have a webgrid to display. I also have an edit button as one of the columns. If Edit button is clicked, few of the column data are shown in edit mode. In edit mode, checkbox as one of the column is shown. Please see below screenshots. Display mode On click of Edit When checkbox is clicked, I am not sure if the value is posted in the correct format or checkbox value is read or not.

Retrieving the checkbox value and showing it with alert but result is [object Object]. Tried to display the JSON stringy value and length shows zero. How to read the checkbox value and pass the same to controller in edit mode. This did not help me fix the issue Following are the other details.

View:

 var gridColumns = new List<WebGridColumn>();

gridColumns.Add(grid.Column("IsActive", "Is Active", format: @<text><span  class="display-mode"><label id="label_IsActive">@item.IsActive</label></span>
<input type="checkbox" id="ChkIsActive" name="Updatechkbox" class = "edit-mode" @if (item.IsActive){ <text>checked="checked"</text>} value="@item.IsActive" ></text>, style: "col2Width"));

gridColumns.Add(grid.Column(null, format: @<text>  <span class="display-mode">@item.USER_ID </span>
        <label id="rowID" class="edit-mode">@item.USER_ID</label>
        </text>, style: "colHide"));

Ajax:

$('.save-user').on('click', function () {

        var tr = $(this).parents('tr:first');
        var rowID = tr.find("#rowID").html();
        var IsDisabled = $('ChkIsActive').find('input[type="checkbox"]:checked')

        alert(IsDisabled);

        tr.find('.edit-mode, .display-mode').toggle();

 var ExcelModel =
        {
            "USER_ID": rowID,              
            "IsActive": IsDisabled,
        };

        $.ajax({
            url: myHostName + '/Configuration/ChangeData',
            data: JSON.stringify(ExcelModel),
            //data: {EmailID: label_EmailID,"IsDisabled": IsDisabled},
            type: 'POST',            
            contentType: 'application/json; charset=utf-8',
            success: function (data) {
                alert(JSON.stringify(ExcelModel));
                alert(data);                  
            }
        });

Controller

     public JsonResult ChangeData(XYZ.Models.Registration model)
    {
        string message = null;
        try
        {
            String result = Someupdate(model);
            if (result.Equals("Success"))
            {//Somecode
            }
        }
        catch (Exception ex)
        {
            //some code
        }
        return Json(message, JsonRequestBehavior.AllowGet);
    }

Aucun commentaire:

Enregistrer un commentaire