Search code examples
javascriptasp.net-mvcrazorengine

how to get the checkbox in edit modal popup in MVC-4


I'm using ajax to get the corresponding row values in modal popup for edit in MVC 4 razor.. for username textbox i get like this...

@Html.TextBoxFor(u => u.useredit.userName,new { @class = "input-xlarge focused", id="Edituname", type = "text" })

if i use same method for checkbox..

@Html.CheckBoxFor(u => u.useredit.isActive, new {id="EditActiv"})

i'm getting plain checkbox.where i gone wrong..or is there any other way for check box,,

Controller: for getting values through ajax

[HttpPost]
    public ActionResult getUserState(int userid)
    {
        TBLAppUser user = new TBLAppUser();
        user = _repository.GetUserByID(userid);
        string[] data = new string[10];
        data[0] = user.userName;
        data[1] = user.firstName;
        data[2] = user.lastName;
        data[3] = user.email;
        data[4] = user.userID.ToString();
        data[5] = user.statusID.ToString();
        data[6] = user.isdelete.ToString();
        data[7] = user.userName;
        data[8] = user.password;
        data[9] = user.isActive.ToString();
        return Json(data);

    }

javascript:

<script type="text/javascript">
function Getuser(_StateId) {
    var url = "/admin/getUserState/";
    $.ajax({
        url: url,
        data: { userid: _StateId },
        cache: false,
        type: "POST",
        success: function (data) {
            $('#Edituname').val(data[0]);
            $('#Editfname').val(data[1]);
            $('#Editlname').val(data[2]);
            $('#Editemail').val(data[3]);
            $('#Editid').val(data[4]);
            $('#state').val(data[5]);
            $('#isdelete').val(data[6]);
            $('#Editname1').val(data[7]);
            $('#Editpsd').val(data[8]);
            $('#EditActiv').val(data[9]);
        },
        error: function (response) {
            alert("error:" + response);
        }

    });
}

view.cshtml:

 @Html.CheckBoxFor(u => u.useredit.isActive, new {id="EditActiv"})

i'm getting the unchecked checkbox in edit popup..pls smeone help me..thnks in advance...


Solution

  • For your view

     @Html.CheckBoxFor(u => u.useredit.isActive, new {id="EditActiv"})
    

    Make the below changes and see if it working

    [HttpPost]
    public ActionResult getUserState(int userid)
    {
        data[9] = user.isActive.ToString()
        return Json(data);
    }
    

    Try use prop() method

     <script type="text/javascript">
     function Getuser(_StateId) {
      var url = "/admin/getUserState/";
      $.ajax({
        url: url,
        data: { userid: _StateId },
        cache: false,
        type: "POST",
        success: function (data) {
        var editCheck=(data[9]=== 'true')?true:false;
        $('#EditActiv').prop('checked',editCheck); //use prop()
       }
    
      });