Question

I Have two simple model Model1, Model2 as 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; }
}

I Have a BigModel contains two other model as:

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 ModelState.IsValid is always true? though data annotations are set. and How can I validate two models in one action?

Was it helpful?

Solution

Please don't use above way.Always try to use ViewModel with your views.Put all your data annotations on that ViewModel and check that inside the action method.

Plese check below mentioned sample ViewModel 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);
            }
        }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top