使用字典进行模型验证

发布于 2024-09-10 15:46:30 字数 1376 浏览 6 评论 0原文

假设我有一个像这样的模型:

public class MyViewModel {
  //some properties
  public string MyString {get;set;}
  public Dictionary<string,string> CustomProperties {get;set;}
}

我正在呈现这样的字典属性:

<%= Html.EditorFor(m => m.CustomProperties["someproperty"]) %>

一切都运行良好,但是我已经实现了一个自定义验证器来验证该字典的属性,但是当返回 ModelValidationResult我无法正确引用成员名称(我相信应该是 CustomProperties[someproperty] )。列表中作为属性的所有项目都正确绑定到它们的错误(我希望文本框中的错误类,以便我可以突出显示它)。

这是到目前为止我的自定义验证器的代码,

public class CustomValidator : ModelValidator
{
    public Custom(ModelMetadata metadata, ControllerContext controllerContext) : base(metadata, controllerContext)
    {
    }

    public override IEnumerable<ModelValidationResult> Validate(object container)
    {
        if (Metadata.PropertyName.Equals("mystring", StringComparison.OrdinalIgnoreCase))
        {
            yield return new ModelValidationResult() {Message = "normal property validator works!!"};
        }
        else if (Metadata.PropertyName.Equals("customproperties", StringComparison.OrdinalIgnoreCase))
        {

            yield return new ModelValidationResult() { MemberName = "CustomProperties[someproperty]", Message = "nope!" };
        }
    }
}

看起来好像有一些东西正在进一步填充 MemberName 属性,并忽略我在那里放入的内容

干杯, 阿马尔

Say I have a model like so:

public class MyViewModel {
  //some properties
  public string MyString {get;set;}
  public Dictionary<string,string> CustomProperties {get;set;}
}

And I am presenting the dictionary property like this:

<%= Html.EditorFor(m => m.CustomProperties["someproperty"]) %>

All is working well, however I have implemented a custom validator to validate the properties of this dictionary, but when returning a ModelValidationResult I can not get the member name referenced correctly (which chould be CustomProperties[someproperty] I believe). All the items in the list which are properties are bound correctly to their errors (I want the error class in the text box so I can highlight it).

Here is my code for the custom validator so far

public class CustomValidator : ModelValidator
{
    public Custom(ModelMetadata metadata, ControllerContext controllerContext) : base(metadata, controllerContext)
    {
    }

    public override IEnumerable<ModelValidationResult> Validate(object container)
    {
        if (Metadata.PropertyName.Equals("mystring", StringComparison.OrdinalIgnoreCase))
        {
            yield return new ModelValidationResult() {Message = "normal property validator works!!"};
        }
        else if (Metadata.PropertyName.Equals("customproperties", StringComparison.OrdinalIgnoreCase))
        {

            yield return new ModelValidationResult() { MemberName = "CustomProperties[someproperty]", Message = "nope!" };
        }
    }
}

It appears like something is filling in the MemberName property further up, and ignoring what I put in there

Cheers,
Amar

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

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

发布评论

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

评论(1

御守 2024-09-17 15:46:30

在我看来,你让验证变得比需要的更加困难。您是否看过框架中内置的 DataAnnotations? Scott Gu 的博客讲座关于这个。这是一种非常好的(而且简单的)模型验证方法。

It appears to me that you are making validation more difficult than it needs to be. Have you taken a look at DataAnnotations which are built into the framework? Scott Gu's blog talks about this. It's a really nice (and easy) way to do validation of models.

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