我的Razor视图中有一个单个表单的两个按钮,我想根据单击哪个按钮来设置一个模型属性以提交表单。下面的代码解释了为什么我需要两个不同的按钮来提交一个表单。
我的剃刀视图中的表格:
@model BTSWeb.Models.DropDownModel
@using (Html.BeginForm("BillAccounts", "Home", FormMethod.Post, new { id="onDemandForm"}))
{
<span style="margin-left:315px"></span>
@Html.TextBoxFor(x => x.server1)
<br/> <br/>
<label style="color:black">
<span style="margin-left:40px"></span>
@Html.TextBoxFor(x => x.billText)
@Html.HiddenFor(x => x.billchecker, new { id ="billchecker"})
<span style="margin-left:0px"></span>
<input id="Button4" type="button" value="On-Demand Bill" onclick="submitOnDemandForm()" />
<span style="margin-left:50px"></span>
<input id="Button5" type="button" value="Force Bill" onclick="submitForceBillForm()" />
<br/> <br/>
}
Jquery函数绑定到上面按钮的onclick:
<script type="text/javascript">
$(document).ready(function () {
$("#onDemandForm>input").addClass("importGroupOnDemandBill");
});
function submitOnDemandForm() {
$("#ddlform>input.importGroupOnDemandBill").remove();
var $inputs3 = $("#onDemandForm>input").clone();
//var $inputs2 = $("#server2form>input").clone();
$("#ddlform").append($inputs3);
//$("#ddlform").append($inputs2);
$inputs3.css('display', 'none');
//submit
$('#ddlform').attr('action', '/Home/BillAccounts');
document.getElementById("ddlform").submit();
}
function submitForceBillForm() {
$("#ddlform>input.importGroupOnDemandBill").remove();
var $inputs4 = $("#onDemandForm>input").clone();
//var $inputs2 = $("#server2form>input").clone();
$("#ddlform").append($inputs4);
//$("#ddlform").append($inputs2);
$inputs4.css('display', 'none');
//submit
$('#ddlform').attr('action', '/Home/BillAccounts');
document.getElementById("ddlform").submit();
}
</script>
我的dropdownview模型中有一个属性billcheker,我想将其设置为&#34; 1&#34;如果点击了Button4,那么&#34; 2&#34;如果在表单提交后单击了Button5。
答案 0 :(得分:1)
添加一个&#34;隐藏字段&#34;对于html中的billcheker属性,如下所示:
@Html.HiddenFor(x => x.billcheker)
在jQuery中设置此文本框的值,如下所示:
function submitOnDemandForm() {
$("#ddlform>input.importGroupOnDemandBill").remove();
var $inputs3 = $("#onDemandForm>input").clone();
//var $inputs2 = $("#server2form>input").clone();
$("#ddlform").append($inputs3);
//$("#ddlform").append($inputs2);
$inputs3.css('display', 'none');
document.getElementById("billcheker").value = "1"; // set value of hidden field
//submit
$('#ddlform').attr('action', '/Home/BillAccounts');
document.getElementById("ddlform").submit();
}
function submitForceBillForm() {
$("#ddlform>input.importGroupOnDemandBill").remove();
var $inputs4 = $("#onDemandForm>input").clone();
//var $inputs2 = $("#server2form>input").clone();
$("#ddlform").append($inputs4);
//$("#ddlform").append($inputs2);
$inputs4.css('display', 'none');
document.getElementById("billcheker").value = "2"; // set value of hidden field
//submit
$('#ddlform').attr('action', '/Home/BillAccounts');
document.getElementById("ddlform").submit();
}