我是jquery ajax的新手。我很想學習客戶端jQuery驗證,而不使用DataAnnotations。當我按下登錄按鈕時,頁面刷新不會使用ajax帖子發佈到控制器的登錄(用戶x)方法。我不是專家任何意見和建議將不勝感激。MVC驗證併發布到控制器與jquery ajax
我的模型
public class User
{
public String UserName { get; set; }
public String Password { get; set; }
}
我控制器
public class LoginController : Controller
{
[HttpPost]
public bool Login(User x)
{
return false;
}
public ActionResult LoginTst()
{
return View();
}
}
的觀點是LoginTst &腳本
$(document).ready(function()
{
$("#signin").click(function()
{
$("#loginForm").validate({
rules: {
UserName: {
required: true,
minlength: 2,
maxlength: 15
},
Password: {
required: true,
minlength: 5,
maxlength: 18
}
},
messages: {
UserName: {
required: "username req",
minlength: "user is small",
maxlength: "user is long"
},
Password: {
required: "pass req",
minlength: "pass is small",
maxlength: "pass is long"
}
},
submitHandler: function (form) {
var form = $('#loginForm');
$.ajax({
cache: false,
async: true,
type: "POST",
url: form.attr('Login'), // the action it
data: form.serialize(),
success: function (data) {
if (data == false) {
alert("Username doesn't exist");
}
}
});
}
});
});
});
<form action="" name="loginForm" id="loginForm">
<div class="form-group">
<input type="text" class="form-control" placeholder="Username" id="UserName" name="UserName">
</div>
<div class="form-group">
<input type="password" class="form-control" placeholder="Password" id="Password" name="Password">
</div>
<div class="form-group">
<button class="btn btn-warning btn-block" type="submit" id="signin">login</button>
</div>
</form>
我在做什麼錯?
你的行爲沒有設置任何東西。 – axlj