如何在剃须刀上进行全局模型更改

时间:2019-03-19 19:41:44

标签: c# razor

我正在尝试使用模式将一些数据传递到控制器中。页面上有多个模态,我发现只有第一个模态的数据才被传递,无论数据是否在另一个模态中进行编辑。

<div id="mark-reassigned-modal" class="modal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <h4 id="myModalLabel">@Employee_Index.MarkReassignedHeading</h4>
            </div>
            <div class="modal-body">
                <div class="alert alert-warning">
                    @Employee_Index.MarkReassignedText
                </div>
                @Html.EditorFor(m => m.DateTimePicker, new { htmlAttributes = new { name = "DateTimePicker", @style = "width:50%;" } })

                @Html.TextAreaFor(m => m.DispositionNotes, new { cols = "50%", rows = "3" })

            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-danger dirtyignore" aria-hidden="true" id="mark-reassigned-submit">@Employee_Index.YesExcuseText</button>
                <a href="#" class="btn btn-link cancel dirtyignore" data-dismiss="modal" aria-hidden="true">@Common.Cancel</a>
            </div>
        </div>
    </div>
</div>

<div id="mark-new-shift-modal" class="modal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <h4 id="myModalLabel">@Employee_Index.MarkNewShiftHeading</h4>
            </div>
            <div class="modal-body">
                <div class="alert alert-warning">
                    @Employee_Index.MarkNewShiftText
                </div>
                @Html.EditorFor(m => m.DateTimePicker, new { htmlAttributes = new { name = "DateTimePicker", @style = "width:50%;" } })

                @Html.TextAreaFor(m => m.DispositionNotes, new { cols = "50%", rows = "3" })

            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-danger dirtyignore" aria-hidden="true" id="mark-new-shift-submit">@Employee_Index.YesExcuseText</button>
                <a href="#" class="btn btn-link cancel dirtyignore" data-dismiss="modal" aria-hidden="true">@Common.Cancel</a>
            </div>
        </div>
    </div>
</div>

用于此的js如下:

$(document).on("click", "#mark-new-shift-submit", function () {
    var resultsContainer = $('#results');
    resultsContainer.block({
        message: '<i class="fa fa-icon fa-spin fa-spinner bigger-230" />'
    });
    var data = $('#Index').serialize();
    $.ajax({
        type: "POST",
        url: '@Url.Action("MarkNewShift", "Employee")',
        data: data,
        dataType: "html",
        success: function (data) {
            $('#mark-new-shift-modal').modal('hide');
            $("#results").html(data);
            updatePagination();
            resultsContainer.unblock();
            $('#employee-list').selectableTable();
            clearSelections();
        }
    });
});

当我检查#Index ID时,我发现DateTimePicker有多个结果,并且仅采用第一个结果。我如何才能使选择器全局化,所以#Index中只有一个结果。如果用户决定使用其他选项,还可以让用户继续从上次停止的地方开始。

1 个答案:

答案 0 :(得分:0)

也许将DateTimePicker和DispositionNotes属性设置为列表是一个主意。

例如:

List<string> DateTimePickers  
List<string> DispositionNotes

然后将它们提供给编辑器,例如

Html.EditorFor(x => x.DateTimePickers[0], ...
Html.EditorFor(x => x.DispositionNotes[0], ...

您甚至可以使用某种类型或使其通用的类型扩展DateTimePicker和DispositionNote类,以便您可以在View中构建一个for循环来创建多个DateTimePickers和DispositionNotes。

赞:

for(int i = 0; i < Model.DateTimePickers.Count; i++)
{
    Html.EditorFor(x => x.DateTimePickers[i], ...
}

尝试一下。