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

发布于 2024-10-05 07:50:01 字数 1324 浏览 0 评论 0原文

我有一个使用 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" >
}

如果我还添加了必需的属性,则仅会调用 NameValidation,如下所示:

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

如何让远程验证工作而不必拥有需要验证吗?

I have a class with data annotations using the Remote attribute:

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

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

In the view I have:

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

The NameValidation only gets called if I also add the Required attribute like this:

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

How do I get the Remote validation to work without having to have the Required validation?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

小…红帽 2024-10-12 07:50:01

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

Update to MVC3 RC2. I just tried it without the Required attribute. Its working.
One thing I found strange though, is that the remote validation is sometimes fired on every key press and onchange the other times

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文