ASP.NET MVC3远程验证不输出错误消息

bal*_*dre 2 validation asp.net-mvc jquery-validate asp.net-mvc-3

我有一个简单的模型,默认情况下RegisterModel,基于该模型创建一个View,Create我最终拥有

public class RegisterModel
{
    [Required]
    [Remote("UserNameExists", "Account", "", ErrorMessage = "Username is already taken.")]
    [Display(Name = "Username (spaces will be stripped, must be at least 6 characters long)")]
    public string UserName { get; set; }

    [Required]
    [Editable(true)]
    [Display(Name = "First and Last name")]
    public string Name { get; set; }

    [Required]
    [DataType(DataType.EmailAddress, ErrorMessage = "You need to enter a valid email")]
    [Remote("EmailExists", "Account", "", ErrorMessage = "Email is already taken.")]
    [Display(Name = "Email address")]
    public string Email { get; set; }

    //[Required]
    //[ValidatePasswordLength]
    [DataType(DataType.Password)]
    [Display(Name = "Create a password")]
    public string Password { get; set; }

    [DataType(DataType.Password)]
    [Display(Name = "Verify password")]
    //[Compare("Password", ErrorMessage = "Password's do not match.")]
    public string ConfirmPassword { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

并在视图中:

<h3>
    Details</h3>
@using (Html.BeginForm("GenerateBetaLink", "Account", FormMethod.Post, new { @id = "beta-user" }))
{
    @Html.ValidationSummary(true)
    <div>
        <div class="editor-label">
            @Html.LabelFor(model => model.UserName)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.UserName) 
            @Html.ValidationMessageFor(model => model.UserName)
        </div>
        <div class="editor-label">
            @Html.LabelFor(model => model.Name)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Name) 
            @Html.ValidationMessageFor(model => model.Name)
        </div>
        <div class="editor-label">
            @Html.LabelFor(model => model.Email)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Email) 
            @Html.ValidationMessageFor(model => model.Email)
        </div>
        <p>
            &nbsp;</p>
        <p>
            <input type="submit" value="Create Beta User" class="btn-submit" />
            <span class="loading"></span>
        </p>
    </div>
}
Run Code Online (Sandbox Code Playgroud)

我的 Validation Controller

public class ValidationController : Controller
{
    public JsonResult UserNameExists(string UserName)
    {
        OnlineServicesRepository db = new OnlineServicesRepository();

        var user = db.FindUserByUsername(UserName.Trim());
        return user == null ?
            Json(true, JsonRequestBehavior.AllowGet) :
            Json(string.Format("{0} is not available.", UserName),
                JsonRequestBehavior.AllowGet);
    }

    public JsonResult EmailExists(string Email)
    {
        OnlineServicesRepository db = new OnlineServicesRepository();

        var user = db.FindUserByEmail(Email.Trim());
        return user != null ?
            Json(true, JsonRequestBehavior.AllowGet) :
            Json(string.Format("{0} is not available.", Email),
                JsonRequestBehavior.AllowGet);
    }
}
Run Code Online (Sandbox Code Playgroud)

我的问题是该远程确认不火,但不写任何东西到错误消息,因为它应该加上,jQuery的方法.valid() 总是告诉我形式是有效的:

http://www.balexandre.com/temp/2011-04-19_0900.png

在这里错过了什么?

MSDN文章显示了相同的代码(在可下载的文件)

Dar*_*rov 7

以下工作对我来说很好:

模型:

public class RegisterModel
{
    [Required]
    [DataType(DataType.EmailAddress, ErrorMessage = "You need to enter a valid email")]
    [Remote("EmailExists", "Home", "")]
    [Display(Name = "Email address")]
    public string Email { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

控制器:

public class HomeController : Controller
{
    public ActionResult Index()
    {
        return View();
    }

    [HttpPost]
    public ActionResult Index(RegisterModel model)
    {
        return View(model);
    }

    public ActionResult EmailExists(string email)
    {
        if ((email ?? string.Empty).Contains("foo"))
        {
            return Json(email + " is not available", JsonRequestBehavior.AllowGet);
        }

        return Json(true, JsonRequestBehavior.AllowGet);
    }
}
Run Code Online (Sandbox Code Playgroud)

视图:

@model RegisterModel

<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>

@using (Html.BeginForm())
{
    @Html.LabelFor(model => model.Email)
    @Html.EditorFor(model => model.Email) 
    @Html.ValidationMessageFor(model => model.Email)
    <input type="submit" value="OK" />
}
Run Code Online (Sandbox Code Playgroud)