在带有Partial视图的MVC 5中,将提交按钮移动到表单外部
Moving the submit button outside the form in MVC 5 with Partial view
在MVC 5中,
我有一个对话框要编辑。
"内容"区域是从"局部"视图创建的。
如果我在部分视图中的表单中有提交按钮,它就可以工作了。但我希望Save按钮位于对话框的按钮上,因此位于窗体和Partial视图之外。
现在的问题是表单数据无法发布。你会怎么解决这个问题?
我有两个想法:1) 将窗体包裹在"局部"视图之外2) 发出AJAX请求而不使用帮助程序,并以某种方式从表单中收集数据?感觉不太对劲。
对话框:
<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>
}
您可以通过指定form
属性(仅限HTML5)将提交按钮放置在表单标记之外
<form .... id="editform">
....
</form>
<input type="submit" form="editform" />
请注意,如果提交按钮具有值属性,它将不会被退回。
另一种选择可能是使用css来定位它。
只需使用Javascript提交表单:
function SubmitMyForm(){
document.getElementById("myForm").submit();
}
HTML:
<input type="button" value="Calculate" onclick="SubmitMyForm()" />
我想您正在寻找:document.myform.submit();
以下是本页中经过Fiddle改编的代码形式。
相关文章:
- 使用按钮和单选按钮提交表单,但始终使用相同的值
- 使用回车按钮提交文本框表单
- 清除按钮提交后的文本区域
- 单击按钮提交输入字段值
- 我可以从 Rails 4 中的按钮提交传递参数吗?
- 如何使用inputtype=按钮提交表单
- 使用两个按钮(提交按钮和普通按钮),它们应该以不同的方式提交相同的表单
- 使用jquery UI next按钮提交输入字段值,php将返回结果
- AJAX-按钮提交以更新表单
- 在向导表单中使用按钮提交值的最佳方式
- 如何阻止按钮提交
- 使用提交按钮提交表单,而不是页面上的其他按钮
- 用于两个功能的Javascript按钮.提交&单击
- 如何使用一个提交按钮提交三个表单
- 如何使用按钮提交表单
- 如何查看从 javascript 中的表单按钮提交调用的 php 文件中的回显
- 不能在按钮提交处理程序中使用变量
- 由多个按钮提交的一个模式引导表单
- 为什么<输入类型=“按钮”>提交表单
- 使用表单外的按钮提交表单