C# 将子属性添加到子属性失败

C# 将子属性添加到子属性失败,c#,asp.net-mvc,viewmodel,fluentvalidation,C#,Asp.net Mvc,Viewmodel,Fluentvalidation,我是FluentValidation的新手 我有两节课 public class A { public B Prop { get; set; } } public class B { public decimal? Example { get; set; } public decimal? Example2 { get; set; } } 我试图验证A类中的B属性 如果Prop的任何属性存在验证错误,我希望将第一个错误设置为Prop object 我正在尝试一些像这

我是FluentValidation的新手

我有两节课

public class A
{
    public B Prop { get; set; }
}

public class B 
{
    public decimal? Example { get; set; }
    public decimal? Example2 { get; set; }
}
我试图验证
A
类中的
B
属性

如果
Prop
的任何属性存在验证错误,我希望将第一个错误设置为Prop object

我正在尝试一些像这样的

public class A : AbstractValidator<A>{    
    public A()
    {
        RuleFor(x => x.Prop.Example).NotEmpty();
        RuleFor(x => x.Prop.Example2).NotEmpty();
        RuleFor(x => x.Prop).Custom((obj,context) => {
            if(/* obj.Example or obj.Example2 has validation error */){
                context.AddFailure("message of any Example or Example2 errors");
            }
        });
    }
}
公共类A:抽象验证器

其中左侧输入为示例,右侧输入为示例2

因此,如果这两种方法中的任何一种都失败了,那么错误消息应该会显示出来(ValidationMessageFor)

我不知道如何验证
RuleFor


这样行吗?我该怎么做?

我认为您可以使用FluentValidation文档为B类创建自己的验证器。

然后在函数的主要作用域中,您可以简单地调用类B的验证器。

您可能可以实现这一点,但您可以通过为类B创建一个验证器,然后将两个验证消息放在UI中彼此紧挨着的位置来获得更轻松的时间:

<div class="col-12 col-lg-6">
    @Html.LabelFor(x => x.Prop, new { @class = "col-form-label" })
    <div class="input-group mb-3">
        @Html.EditorFor(x => x.Prop.Example, new { htmlAttributes = new { @class = "form-control" } })
        <div class="input-group-append">
            <span class="input-group-text">%</span>
            <span class="input-group-text">@@</span>
        </div>
        @Html.EditorFor(x => x.Prop.Example2, new { htmlAttributes = new { @class = "form-control" } })
        <div class="input-group-append">
            <span class="input-group-text">KVA</span>
        </div>
        <div class="invalid-feedback">
            @* Validation messages for both properties of class "B" *@
            @Html.ValidationMessageFor(x => x.Prop.Example)
            @Html.ValidationMessageFor(x => x.Prop.Example2)
        </div>
    </div>
</div>

@LabelFor(x=>x.Prop,新的{@class=“col form label”})
@EditorFor(x=>x.Prop.Example,new{htmlAttributes=new{@class=“form control”})
%
@@
@EditorFor(x=>x.Prop.Example2,new{htmlAttributes=new{@class=“form control”})
千伏安
@*类“B”的两个属性的验证消息*@
@Html.ValidationMessageFor(x=>x.Prop.Example)
@Html.ValidationMessageFor(x=>x.Prop.Example2)

Hi,ty对于答案,我试过了,问题是我没有将错误添加到Props属性中,这样,它只添加到Props.Example和Props.example2为什么希望验证消息与
A.Prop
关联,而不是类
B
中的单个属性?您能否发布显示验证消息显示方式的视图代码?您能描述一下为什么在
Prop
下合并错误更可取吗?您正在与FluentValidation的工作原理进行斗争。谢谢您的时间!我用视图代码更新了答案,我正在尝试这样做,因为我使用viewmodel进行两个输入(显示为1),然后,如果2个输入中的任何一个未完成验证,它应该显示错误(仅为1),您可以在视图中直接使用B类。你为什么没用过它?@HiteshKansagara你好!我不太理解你的问题,你在视图中使用类b是什么意思?你在视图中传递模型,对吗?目前你通过的是A而不是A,你可以通过B获得答案!我考虑过这一可能性,但我的问题是消息可能太长,所以我只想显示一个,如果两者中的任何一个都失败了,是否有可能使它在视图中工作?
<div class="col-12 col-lg-6">
    @Html.LabelFor(x => x.Prop, new { @class = "col-form-label" })
    <div class="input-group mb-3">
        @Html.EditorFor(x => x.Prop.Example, new { htmlAttributes = new { @class = "form-control" } })
        <div class="input-group-append">
            <span class="input-group-text">%</span>
            <span class="input-group-text">@@</span>
        </div>
        @Html.EditorFor(x => x.Prop.Example2, new { htmlAttributes = new { @class = "form-control" } })
        <div class="input-group-append">
            <span class="input-group-text">KVA</span>
        </div>
        <div class="invalid-feedback">
            @* Validation messages for both properties of class "B" *@
            @Html.ValidationMessageFor(x => x.Prop.Example)
            @Html.ValidationMessageFor(x => x.Prop.Example2)
        </div>
    </div>
</div>