0

我有视图:

@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; }
    }
}

什么时候应该触发远程验证方法?当我点击输入按钮?我做错了什么?

4

2 回答 2

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>
于 2012-12-06T20:22:27.410 回答
1

在 CheckPassword 方法中,密码变量与模型密码不匹配。它应该在 TitleCase 中:- CheckPassword(string Password)

于 2014-01-07T18:21:17.227 回答