如果表单未经验证,如何防止将模型发送到Controller?
剃刀
@using (Html.BeginForm("Login", "Login", FormMethod.Post))
{
<div>
<div class="form-group input-group">
<span class="input-group-addon"><i class="glyphicon glyphicon-user"></i></span>
@Html.TextBoxFor(model => model.AccountName, new { @id = "accountNameText", @class = "form-control", @placeholder = "Account name" })
</div>
<div class="form-group input-group">
<span class="input-group-addon"><i class="glyphicon glyphicon-lock"></i></span>
@Html.TextBoxFor(model => model.Password, new { @id = "accountPasswordText", @class = "form-control", @placeholder = "Password" })
</div>
<div class="form-group">
<button type="submit" class="btn btn-success btn-block" onclick="valideLogin()">Login</button>
</div>
<span id="loginStatusMessege" style="color: red">
</span>
</div>
}
jQuery的
function valideLogin() {
var accName = $('#accountNameText');
var accPass = $('#accountPasswordText');
if (accName.val() === '' || accName.val() === '') {
$('#loginStatusMessege').html('Account name and password cannot be blank.');
}
}
在我按下登录按钮并且文本框未填充之后,代码成功进入jQuery并就错误进行了响应,但是仍然每次我都进入Controller。如何防止此动作?
最佳答案
尝试添加return false;
:
function valideLogin() {
var accName = $('#accountNameText');
var accPass = $('#accountPasswordText');
if (accName.val() === '' || accName.val() === '') {
$('#loginStatusMessege').html('Account name and password cannot be blank.');
return false;
}
}
关于javascript - 如何防止将数据传递到 Controller ,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/34900781/