【发布时间】:2014-06-26 00:32:56
【问题描述】:
我试图在我的创建视图上提交数据时显示一个模式对话框。我需要模态来强制提交等待用户在模态对话框上确认。然后需要提交数据。下面是我使用 bootstrap 3.0 modal-dialog 创建视图的代码:
<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>
}
这确实会中断提交功能并调出模式对话框,但我不知道一旦在对话框上做出选择后如何将数据提交回控制器。我还尝试使用带有以下脚本的 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>
这确实会打开模态对话框,但我仍然无法将模型数据提交回控制器。任何帮助将不胜感激。
更新
通过检查生成的 html,我发现我的输入字段位于我没有放在那里的表单中。我怀疑这是因为我使用了 @Html.ValidationSummary(true) 或 MVC 内置的东西。所以不要使用:
$("#create").submit();
我用过:
$("form:first").submit();
在页面上提交第一个也是唯一一个表单,它成功了!
【问题讨论】:
标签: asp.net-mvc jquery-ui modal-dialog submit