MVC multiple ViewModel and ModelState

I have two simple models Model1, Model2 as shown below:

public class Model1
{
    public int Id { get; set; }
    [Required]
    public string Name { get; set; }
}
public class Model2
{
    public int Id { get; set; }
    [Required]
    public string Code { get; set; }
}

My BigModel contains two other models:

public class BigModel 
{
    public BigModel()
    {
        Model1 = new Model1 ();
        Model2 = new Model2();
    }
    public Model1 Model1 { get; set; }
    public Model2 Model2 { get; set; }
}

and in my controller:

public ActionResult Register(BigModel bigModel)
    {            
        if (ModelState.IsValid)
        {
            //do somthing
            return RedirectToAction("Index");
        }            
        return View(bigModel);
    }

My question is: why is ModelState.IsValid always right? although data annotations are specified. and how can I test two models in one action?

+3
source share
1 answer

Please do not use the above method. Always try to use ViewModel with your views. Put all the data annotations on this ViewModel and verify that inside the action method.

Plese checking the example ViewModel below as an example.

 public class ProductViewModel
    {
        public Guid Id { get; set; }

        [Required(ErrorMessage = "required")]
        public string ProductName { get; set; }

        public int SelectedValue { get; set; }

        public virtual ProductCategory ProductCategory { get; set; }

        [DisplayName("Product Category")]
        public virtual ICollection<ProductCategory> ProductCategories { get; set; }
    }

Inside the action method:

        [HttpPost]
        public ActionResult AddProduct(ProductViewModel productViewModel) //save entered data
        {
            //get product category for selected drop down list value
            var prodcutCategory = Repository.GetProductCategory(productViewModel.SelectedValue);

            //for get all product categories
       var prodcutCategories = Repository.GetAllProductCategories();

            //for fill the drop down list when validation fails 
             productViewModel.ProductCategories = prodcutCategories;

            //for initialize Product domain model
            var productObj = new Product
                                     {
                                         ProductName = productViewModel.ProductName,
                                         ProductCategory = prodcutCategory,
                                     };

            if (ModelState.IsValid) //check for any validation errors
            {
                //save recived data into database
                Repository.AddProduct(productObj);
                return RedirectToAction("AddProduct");
            }
            else
            {
                //when validation failed return viewmodel back to UI (View) 
                return View(productViewModel);
            }
        }

: . , .

ViewModel ASP.NET MVC?

+1

All Articles