0

我制作了可编辑的 webgrid,当我为 webgrid 传递值时,我总是为复选框提供默认值。当数据库中的值为“TRUE”时,将复选框编辑为 false 后,值将像数据库中的值一样传递“TRUE”接缝。有人能帮我吗

这是我的看法

<div>
    @{
        var grid = new WebGrid(Model.DataDiriList, rowsPerPage: 15, canPage: true, canSort: true, ajaxUpdateContainerId: "gridContent");
        @grid.GetHtml(
                           tableStyle: "row",
                  alternatingRowStyle: "alt",
                  columns: grid.Columns(
                  grid.Column("ID", format: @<text>  <span  class="display-mode">@item.ID </span> <label id="UserID" class="edit-mode">@item.ID</label> </text>, style: "col1Width" ),
                  grid.Column("Nama", "Nama", format: @<text>  <span  class="display-mode"> <label id="lblNama"  >@item.Nama</label> </span> <input type="text" id="Nama" value="@item.Nama" class="edit-mode" /></text>, style: "col2Width"),
                  grid.Column("Umur", "Umur", format: @<text>  <span  class="display-mode"> <label id="lblUmur"  >@item.Umur</label> </span> <input type="text" id="Umur" value="@item.Umur" class="edit-mode" /></text>, style: "col2Width"),
                  grid.Column(header: "Active", format:@<text> <span  class="display-mode"> @Html.Raw("<input type=\"checkbox\" " + ((item.Active == true) ? "checked='cheked'" : "") + "disabled = 'disabled'/>") </span> <input id="chkActive" type="checkbox" @(item.Active ? " checked=checked" : null) name="CloseSelling" value="@item.Active" class="edit-mode" /></text>, style: "col2Width"),
                  grid.Column("Action", format: @<text>
                                <button class="edit-user display-mode" >Edit</button>
                                <button class="save-user edit-mode"  >Save</button>
                                <button class="cancel-user edit-mode" >Cancel</button>
                            </text>,  style: "col3Width" , canSort: false)
                 ));
    }
</div>

@section scripts
    {
    <script type="text/javascript">

        $(document).ready(function () {
            $('.edit-mode').hide();
            $('.edit-user, .cancel-user').on('click', function () {
                var tr = $(this).parents('tr:first');
                tr.find('.edit-mode, .display-mode').toggle();
            });



            $(".save-user").on("click", function () {
                var tr = $(this).parent().parent();
                var t = tr.find("#Nama").val();
                var Umur = tr.find('#Umur').val();
                var ID = tr.find('#UserID').html();
                var chkActive = tr.find('#chkActive').val();

                $.ajax({
                    url: '@Url.Action("UpdateUser", "Home")',
                type: "Post",
                data: { CustomerNameId: t, UserID: ID, Umur: Umur,Active: chkActive},
                dataType: 'json',
                success: function (result) {
                    $("#mygrid").html('');
                    $("#mygrid").html(result);
                }
                });

                tr.find("#lblNama").text(t);
                tr.find("#lblUmur").text(Umur);
                tr.find('.edit-mode, .display-mode').toggle();
        });

    });
</script>
    }

我的控制器

public JsonResult UpdateUser(string CustomerNameId, string UserID, string Umur, bool Active)
        {
            var ID = Convert.ToInt32(UserID);
            var IntUmur = Convert.ToInt32(Umur);
            var dd = db.DataDiris.AsQueryable().Where(r => r.ID == ID).Single();
            dd.ID = ID;
            dd.Nama = CustomerNameId;
            dd.Umur = IntUmur;

            db.Entry(dd).State = EntityState.Modified;
            db.SaveChanges();
            string message = "Success";
            return Json(message, JsonRequestBehavior.AllowGet);
        }
4

0 回答 0