How to use html.ValidationMessageFor

后端 未结 1 351
渐次进展
渐次进展 2021-02-01 05:42

I\'m trying to get my view to give me the error message next to the text box if a user enters something invalid (like a string where it\'s expecting a number). Instead, I\'m ge

相关标签:
1条回答
  • 2021-02-01 06:26

    It was silly simple.....I just didn't add the ErrorMessage field as part of the [Required] decorator. For example:

    [Required(ErrorMessage = "First name is required")]
    [StringLength(30, ErrorMessage = "Name can be no larger than 30 characters")]
    public string F_Name { get; set; }
    
    [Required(ErrorMessage = "Last name is required")]
    [StringLength(30, ErrorMessage = "Name can be no larger than 30 characters")]
    public string L_Name { get; set; }
    

    Now, if a user either doesn't enter anything in the name fields, or enters something over 30 characters, the Post method doesn't get executed and the user gets a little message telling them what's wrong.

    0 讨论(0)
提交回复
热议问题