2012-12-06 83 views
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; } 
    } 
} 

當應該遠程驗證方法火?當我點擊輸入按鈕?我做錯了什麼?

+0

解決方案找到[這裏] [1] - 需要設置在web.config中 [1]:http://stackoverflow.com/questions/19680967 /遠程attribue驗證 - 不點火式-ASP淨MVC – netmajor

回答

2

你在視圖/剃刀布局中缺少不引人注目的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

在CheckPassword方法密碼變量與模型密碼不匹配。它應該在的首字母大寫: - CheckPassword(字符串密碼)