Testing ModelState is always valid in asp.net mvc

后端 未结 7 1107
野趣味
野趣味 2020-12-09 16:29

When testing my controller\'s actions the ModelState is always valid.

public class Product
{
    public int Id { get; set; }

    [Required]
    [StringLengt         


        
7条回答
  •  春和景丽
    2020-12-09 16:46

    Validation happens when the posted data is bound to the view model. The view model is then passed into the controller. You are skipping part 1 and passing a view model straight into a controller.

    You can manually validate a view model using

    System.ComponentModel.DataAnnotations.Validator.TryValidateObject()
    

提交回复
热议问题