我有查看:
@model MvcApplication2.Models.HomeModel
@{
ViewBag.Title = "Home";
}
<h2>Home</h2>
<a>Witaj</a>
@Model.UserName
@using (Html.BeginForm())
{
<a>Podaj hasło</a>
@Html.PasswordFor(m => m.Password)
@Html.ValidationMessageFor(x => x.Password)
<input type="submit" />
}
和控制器
using System.Web.Mvc;
using System.Web.UI;
using MvcApplication2.Models;
namespace MvcApplication2.Controllers
{
[OutputCache(Location = OutputCacheLocation.None, NoStore = true)]
public class HomeController : Controller
{
public ActionResult Index()
{
HomeModel model = new HomeModel() { UserName = "John" };
return View(model);
}
public JsonResult CheckPassword(string password)
{
bool result = false;
if (password.Length < 4)
{
result = false;
}
else
{
result = true;
}
return Json(result, JsonRequestBehavior.AllowGet);
}
}
}
和型号:
using System.Web.Mvc;
namespace MvcApplication2.Models
{
public class HomeModel
{
public string UserName { get; set; }
[Remote("CheckPassword", "Home", ErrorMessage = "Wpisz lepsze hasło")]
public string Password { get; set; }
}
}
远程验证方法什么时候开火?当我点击输入按钮?我做错了什么?
答案 0 :(得分:2)
您是否在视图/ Razor布局中缺少不引人注目的jQuery引用?
<script src="@Url.Content("~/Scripts/jquery-1.7.1.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.js")" type="text/javascript"></script>
答案 1 :(得分:1)
在CheckPassword方法中,密码变量与模型密码不匹配。它应该在TitleCase中: - CheckPassword(字符串密码)