ASP.Net MVC3远程数据注释不起作用

时间:2010-12-01 23:28:10

标签: data-annotations asp.net-mvc-3

我有一个使用Remote属性进行数据注释的类:

public class Person 
{   
    [Remote("NameValidation","Validation", ErrorMessage = "Field is Invalid", Fields = "LastName")]
    public string FirstName { get; set; }
    public string LastName { get; set; } 
}

在ValidationController中:

    public ActionResult NameValidation(string FirstName, string LastName)
    {
        bool isNameValid = true;

        if (FirstName.Contains("John") && LastName.Contains("Doe"))
        {
            isNameValid = false;
        }

        return Json(isNameValid, JsonRequestBehavior.AllowGet);
    }

在视图中我有:

@{Html.EnableClientValidation(); }
@using (Html.BeginForm())
{ 
    @Html.EditorFor(x => x.FirstName) @Html.ValidationMessageFor(x => x.FirstName)
    @Html.EditorFor(x => x.LastName) @Html.ValidationMessageFor(x => x.LastName)

   <input name="finishButton" type="submit" id="button" >
}

如果我还添加如下所示的Required属性,则仅调用NameValidation:

public class Person 
{   
    [Required]
    [Remote("NameValidation","Validation", ErrorMessage = "Field is Invalid", Fields = "LastName")]
    public string FirstName { get; set; }
    public string LastName { get; set; } 
}

如何在不进行必要验证的情况下使远程验证正常工作?

1 个答案:

答案 0 :(得分:0)

更新到MVC3 RC2。我只是尝试了没有Required属性。它的工作。 我发现奇怪的一件事是,远程验证有时会在每次按键时触发,而在其他时候更改