这是我的设置
HTML
@using (Html.BeginForm("Action", "Controller", FormMethod.Post, new { @class = "form-horizontal", role = "form", enctype = "multipart/form-data", id = "mainForm" }))
{
<div class="form-group">
@Html.LabelForRequired(m => m.Prop1)
@Html.DropDownListFor(m => m.Prop1, (SelectList)ViewBag.SelectList, "Select Role...", new { @class = "form-control" })
@Html.ValidationMessageFor(m => m.Prop1, null, new { @class = "help-block" })
</div>
<div class="row">
<div class="col-sm-2 pull-right">
<button type="submit" class="btn btn-default" id="previous">@Html.T("Back")</button>
<button type="submit" class="btn btn-primary">@Html.T(Model.ButtonText)</button>
</div>
</div>
}
查看模型设置
public class ViewModel{
[Required]
public string Prop1 { get; set;}
}
我想要实现的是,当我按下后退按钮时,我想忽略任何表单验证,而只是将用户重定向到控制器中的后退操作。
这是我的JavaScript的样子
$("#previous").off('click').on('click', (evt) => {
var form = $("#mainForm");
if (form.length != 0) {
this.resetFormValidation(form);
}
var actionUrl = $('#mainForm').attr('action');
var indexOfBackslash = actionUrl.lastIndexOf('/');
var newActionUrl = actionUrl.slice(0, indexOfBackslash + 1);
var newActionUrl = newActionUrl + 'BackSubmit';
$('#mainForm').attr('action', newActionUrl);
(<any>$('#mainForm').validate()).cancelSubmit = true;
if (form.length != 0) {
this.resetFormValidation(form);
}
});
}
resetFormValidation(form: any) {
$("#mainForm").find('[data-valmsg-replace]')
.removeClass('field-validation-error')
.addClass('field-validation-valid')
.empty();
$("#mainForm").find('div.has-error').removeClass('has-error');
}
这仍然显示字段验证,并且不进行回发。我想念什么?