asp.net-mvc - 在 MVC 5 中保持提交以进行模式对话框确认

标签 asp.net-mvc jquery-ui modal-dialog submit

当在我的创建 View 上提交数据时,我尝试显示模式对话框。我需要模式来强制提交等待模式对话框上的用户确认。然后需要提交数据。下面是我使用 bootstrap 3.0 modal-dialog 创建 View 的代码:

<div class="form-group">
                @Html.LabelFor(model => model.Name, new { @class = "control-label col-md-2" })
                <div class="col-md-10">
                    @Html.EditorFor(model => model.Name)
                    @Html.ValidationMessageFor(model => model.Name)
                </div>
            </div>
<div class="col-md-offset-2 col-md-10">
                <input id="create" type="submit" value="Create" class="btn btn-default"/>
            </div>

</div>

<div id="modalBox" class="modal">
<div class="modal-dialog">
    <div class="modal-content">
        <div class="modal-header">
            <a class="close" data-dismiss="modal">X</a>
            <h1>Confirmation</h1>
        </div>
        <div class="modal-body">
            <h6>Click YES to confirm</h6>
        </div>
        <div class="modal-footer">
            <button id="noPost" class="btn btn-default" name="postConfirm" value="false" data-dismiss="modal">No Thanks</button>
            <button id="yesPost" class="btn btn-primary" name="postConfirm" value="true" data-dismiss="modal">YES</button>
        </div>
    </div>
</div>

@section scripts{
<scripts>
$(function () {
    var modalBox = function (e) {
        e.preventDefault();
        $("#modalBox").modal({ show: true });
    };
    $("#create").click(modalBox);
});
<scripts>
}

这确实会中断提交功能并打开模式对话框,但我不知道在对话框中进行选择后如何将数据提交回 Controller 。我还尝试使用 jquery ui 模式对话框和下面的脚本:

<script>
$("#create").click(function (e) {
    e.preventDefault();
    $("#postModal").dialog("open");
});

$("#postModal").dialog({
    autoOpen: false,
    width: 400,
    resizable: false,
    modal: true,
    buttons: {
        "No Thanks": function (data) {
            $("#create").submit();
            $(this).dialog("close");
        },
        "YES": function () {
            $("#create").submit();
            $(this).dialog("close");
        }
    }
});
</script>

这确实会打开模式对话框,但我仍然无法让它将模型数据提交回 Controller 。任何帮助将不胜感激。

更新

通过检查生成的 html,我发现我的输入字段驻留在我没有放在那里的表单中。我怀疑这是因为我使用了 @Html.ValidationSummary(true) 或 MVC 中内置的东西。因此,不要使用:

$("#create").submit();

我用过:

$("form:first").submit();

在页面上提交第一个也是唯一一个表单,它成功了!

最佳答案

将创建按钮类型更改为 type=button,并使其仅打开模态视图。并在模态 div 持有者中创建 yespost 按钮以输入 Submit 。您也可以尝试将整个模态 div 持有者放入表单组 div 中。在这里,您在打开模态视图之前发布帖子。这是因为你的按钮有 2 个逻辑任务,将这些任务分开,这应该可以工作。

关于asp.net-mvc - 在 MVC 5 中保持提交以进行模式对话框确认,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23544778/

相关文章:

asp.net - Asp.NET MVC View 中的引号

c# - 在 View 之间传递变量

javascript - 使用 Ajax Post 在 MVC 中获取选中的值复选框

javascript - 如何访问 $.widget 选项值?

javascript - 如何在 jQuery 中序列化多个可排序项和子项

javascript - 表格内的可点击按钮

javascript - Jquery UI - 自动完成不是一个函数

css - 滚动到底部时,模态底部的填充会弄乱固定标题

swift - 如何创建或呈现这样的 View Controller (Facebook Modally 方式?)

css - 防止 CSS Modal 滚动到顶部