首先像您拥有的那样构建视图,但不使用助手,而是构建 html 字段。确保在我们可以用作选择器的字段上放置一个 id 或类
<input type="text" class="txtUserName" /> etc
然后确保页面上引用了 jquery 并将其放在屏幕底部
<script type="text/javascript">
$(document).ready(function(){
$('.btnSubmit').on('click', function(){
$.ajax({
url: "@(Url.Action("Action", "Controller")",
type: "POST",
contentType: "application/json",
data: { UserName: $('.txtUserName').val(), Password: $('.txtPassword').val() }
cache: false,
async: true,
success: function (result) {
alert('Login Successful!');
window.location = "@Url.Action("Index", "Controller")";
}
});
});
});
</script>
然后在你的控制器上你需要设置一个方法来接收ajax调用
[HttpPost]
public ActionResult Login(string UserName, string Password){
//Check the user name and password against the database
//from here http://stackoverflow.com/questions/10608198/asp-net-mvc3-returning-success-jsonresult
var result=new { Success="True", Message="Success"};
return Json(result, JsonRequestBehavior.AllowGet);
}