简体   繁体   中英

How to Add Metadata to model in View by fluent validation in Entity Framework?

I have an ASP.NET MVC project. I have a model such this:

[Validator(typeof(PersonValidation))]
public class Person 
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string Family { get; set; }
}

I have an strong view by above model.

I create a validator by fluent validation for my model. this is my code :

 public PersonValidation()
 {
        RuleSet("Update", () =>
        {
            RuleFor(t => t.Name).NotNull().WithMessage("Error Null Name").When(t => t.ModelStatus == ModelStatus.Update);
        });
}

I want to create a RuleSet that add a metadata to some of my models field such Name in html file(View). How can I do that?

Use WithName extension to specifies a custom property name to use within the error message.

public PersonValidation()
     {

                RuleFor(t => t.Name).NotNull().WithName("Name").WithMessage("Error Null Name");
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM