在MVC 5中,我有一个编辑对话框。内容区域是由一个局部视图创建的。如果将提交按钮放在局部视图中的表单内,它可以正常工作。但是我想把保存按钮放在对话框底部,因此在表单和局部视图之外。现在问题是表单数据无法提交。您会如何解决这个问题?我有两个想法:
1) 将表单包装在局部视图之外
2) 进行AJAX请求,不使用helper,并从表单中收集数据?感觉不是正确的方法。
对话框:
<div id="myModal" class="modal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
<h4 class="modal-title" id="myModalLabel">Edit database connection</h4>
</div>
<div class="modal-body">
@{ Html.RenderPartial("View"); }
</div>
<div class="modal-footer">
<button id="saveBtnSettings" type="button" class="btn btn-primary">Save</button>
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
Javascript对话框
var saveSettings = function () {
var url = buildUrl("Edit", "Setting");
$.ajax({
type: "POST",
url: url
})
.done(function (data, textStatus, jqXhr) {
alert('done' + data);
})
.fail(function (jqXhr, textStatus, errorThrown) {
alert(textStatus.toUpperCase() + ": " + errorThrown + 'Could not load html. ');
});
};
局部视图
@using (Ajax.BeginForm("Edit", "Setting", new AjaxOptions { UpdateTargetId = "div" }))
{
<fieldset>
@Html.AntiForgeryToken()
<div class="form-horizontal">
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(model => model.User, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.User, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.User, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.DataSource, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.DataSource, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.DataSource, "", new { @class = "text-danger" })
</div>
</div>
</div>
</fieldset>
}
部分视图 - 在表单内使用按钮
@using (Ajax.BeginForm("Edit", "Setting", new AjaxOptions { UpdateTargetId = "div" }))
{
<fieldset>
@Html.AntiForgeryToken()
<div class="form-horizontal">
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(model => model.User, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.User, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.User, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.DataSource, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.DataSource, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.DataSource, "", new { @class = "text-danger" })
</div>
</div>
<p>
<input type="submit" value="Calculate" /> @* WORKS WITH BUTTON INSIDE OF FORM *@
</p>
</div>
</fieldset>
}