Javascript 在ASP.NETMVC中单击一个按钮提交两个表单
我的MVC web应用程序中有两个不同视图中的两个表单: 第一个视图中的第一个表单是dropdownlist,其id为“ddlform”:Javascript 在ASP.NETMVC中单击一个按钮提交两个表单,javascript,asp.net-mvc,razor,forms,Javascript,Asp.net Mvc,Razor,Forms,我的MVC web应用程序中有两个不同视图中的两个表单: 第一个视图中的第一个表单是dropdownlist,其id为“ddlform”: @model BTSWeb.Models.DropDownModel @使用(Html.BeginForm(“Index”,“Home”,FormMethod.Post,new{id=“ddlform”})) { @Html.DropDownListFor(x=>x.SelectedStateId,Enum.GetNames(typeof(BTSWeb.Mo
@model BTSWeb.Models.DropDownModel
@使用(Html.BeginForm(“Index”,“Home”,FormMethod.Post,new{id=“ddlform”}))
{
@Html.DropDownListFor(x=>x.SelectedStateId,Enum.GetNames(typeof(BTSWeb.Models.States)).Select(e=>newselectListItem{Text=e,Value=((int)Enum.Parse(typeof(BTSWeb.Models.States),e)).ToString()),new{style=“宽度:60px;字体大小:105%;边框半径:6.5px 6.5px 6.5px 6.5px;左侧填充:5px”})
}
第二个视图中的表单,id为“serverform”和两个文本框:
@model BTSWeb.Models.DropDownModel
@using (Html.BeginForm("Index", "Home", FormMethod.Post, new { id="serverform"}))
{
<label style="color:black"> Type the name of the server you want to use.
</label>
<span style="margin-left:80px"></span>
@Html.TextBoxFor(x => x.server1)
<br/><br/>
<span style="padding-top:2px"></span>
<label style="color:black"> If you wish you compare the results from two servers. Type the name of second server:
</label>
@Html.TextBoxFor(x => x.server2)
}
@model BTSWeb.Models.DropDownModel
@使用(Html.BeginForm(“Index”,“Home”,FormMethod.Post,new{id=“serverform”}))
{
键入要使用的服务器的名称。
@Html.TextBoxFor(x=>x.server1)
如果希望比较两台服务器的结果,请键入第二台服务器的名称:
@Html.TextBoxFor(x=>x.server2)
}
在绑定到JS函数的第二个视图中提交这两个表单的按钮:
<input id="Button1" type="button" value="Find Billable Account" onclick="submitForms()" />
最后是JS函数,也在第二个视图中:
<script>
function submitForms() {
document.getElementById("serverform").submit();
document.getElementById("ddlform").submit();
}
</script>
函数submitForms(){
document.getElementById(“serverform”).submit();
document.getElementById(“ddlform”).submit();
}
问题是只有ddl表单正在提交,而不是另一个表单。当您提交表单时,当前页面停止执行,然后服务器弹出一个新页面。这就像导航离开一样,只是您要将for数据与请求一起发送到服务器。因此,您不能在同一页上提交两份表格 您可以: 1) 使用AJAX提交一个或两个表单 或 2) 将每个表单指向它自己的Iframe,并自己处理Iframe中的响应
<script>
function submitForms() {
document.getElementById("serverform").submit();
document.getElementById("ddlform").submit();
}
</script>