0

すみません、webgrid を作ろうとしたら、webgrid にチェックボックスを入れました。チェックボックスの値を変更したい。私はwebgridに5行あり、最初の行にはチェックボックスの成功の値が変更されています。ただし、2 行目から 5 行目までは、値が変化する可能性があります。誰かが私に言うことができますか、私の間違いは何ですか?

これが私の見解

<div id="mygrid">
</div>
<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"> <label id="lblActive">@item.ActiveStatus</label> </span> <input id="chkActive" type="checkbox" @((item.Active == true) ? " checked=checked" : "") name="CloseSelling" value="@item.Active" class="edit-mode" onchange="adchange()" /></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();
            });
            $(function () {
                $('#chkActive').click(function () {
                    alert($(this).val() + ' ' + (this.checked ? 'checked' : 'unchecked'));
                });
            });

            $(".save-user").on("click", function () {
                var tr = $(this).parent().parent();
                var t = tr.find("#Nama").val();
                var Umur = tr.find('#Umur').val();
                var chkActive = $('#chk').attr('checked') ? "True" : "False";
                var ID = tr.find('#UserID').html();
                alert(chkActive);

                $.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;
            dd.Active = Active;

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

私を助けてください

ありがとう

4

1 に答える 1

0

チェックボックスと mvc webgrid の問題については
、短いスクリプトを追加するだけです。

これが解決策です:
グラブ スクリプトでは、すべて同じです:

var IsDisabled = tr.find("#IsDisabled").val();

&

tr.find("#lblIsDisabled").text(IsDisabled);

&

"IsDisabled": IsDisabled,

ここまではすべて同じですが、次のようにチェックボックスを追加する必要があります。

grid.Column("IsDisabled", "IsDisabled", format: @<text> <span  class="display-mode"> <label id="lblIsDisabled">@item.IsDisabled</label> </span>  
<input class="edit-mode" id="IsDisabled" name="IsDisabled" type="checkbox" value="@if (item.IsDisabled){<text>True</text>}else{<text>False</text>}" @if(item.IsDisabled){<text>checked</text>}/>
</text>),

代わりに (!) 次のように mvc html ヘルパーを使用することもできます。

@Html.CheckBox("ShowOnChart", (bool)item.ShowOnChart, new { @class = "edit-mode", value = item.ShowOnChart })

最終的に必要なのは、次のスクリプトを追加することだけです。

<script type="text/javascript">
$(function () {
    $('input:checkbox').change(function () {
        if ($(this).attr("checked")) {
            $(this).attr("value", true);
        } else {
            $(this).attr("value", false);
        }
    });
});
$(document).ready(function () {
    $('input[type=checkbox]').each(function () {
        if ($(this).attr("checked")) {
            $(this).attr("value", true);
        } else {
            $(this).attr("value", false);
        }
    });
});
</script>

ページにさらにチェックボックスがあり、それらを除外したい場合は、次を使用します。

$('.edit-mode:checkbox').change(...

コントローラーでboolを受け取るとき、私は行くのが好きです:

bool? isDisabled

ActionResult が常に isDisabled 入力で呼び出されるとは限らない場合に、より表現しやすくするためです。

お役に立てれば。

于 2013-09-11T07:57:29.933 に答える