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

时间:2011-04-19 07:22:22

标签: asp.net-mvc validation asp.net-mvc-3 jquery-validate

我有一个简单的模型是默认的RegisterModel,在根据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; }
}

并在视图中:

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

我的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);
    }
}

我的问题,远程验证会触发,但不会将任何内容写入错误消息,因为它应该是jQuery方法.valid() 一直告诉我 表单有效

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

  

我缺少什么

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

2 个答案:

答案 0 :(得分: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; }
}

控制器:

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

查看:

@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" />
}

答案 1 :(得分:0)

我遇到了同样的问题并通过更新到最新的jQuery(1.6)和jQuery.validate(1.8)库来解决它。获取这些内容的最简单方法是在NuGet中搜索jQuery。