简体   繁体   中英

MVC Html.Checkbox

I have a checkbox, is the case statement from the stored procedure marks the value as 1 I want to have the box checked. I tried this below but with no success.

<td width="2%">
    @if (Model.IsCurrentlyClosed == true)
    {
        @Html.CheckBoxFor(item => checked(item.IsCurrentlyClosed))
    }
    else
    {
        @Html.CheckBoxFor(item => item.IsCurrentlyClosed)
    }

Please try this (for checked):

@Html.CheckBox("IsCurrentlyClosed")

I didn't reproduce the behavior of the code, it's just an idea if you want to record the value:

$('#MyCheck').click(function () {
        var check= new Object();
        check.IsCurrentlyClosed= $(this).closest('tr').find(".CheckClass").val();
        if (status != null) {
                    $.ajax({
                        type: "POST",
                        url: "/Home/EditPost",
                        data: JSON.stringify(check),
                        contentType: "application/json; charset=utf-8",
                        dataType: "json",
                        success: function (response) {
        if (this.checked)            
            alert('I m Checked');
        else
            alert('I m not Checked');
        }
});

public JsonResult EditPost(DataBase CheckData)
    {

        DataBase check= new DataBase()
        {
            IsCurrentlyClosed= CheckData.IsCurrentlyClosed,
        };
        db.Entry(check).State = EntityState.Modified;
        db.SaveChanges();
        return Json(check, JsonRequestBehavior.AllowGet);
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM