2

I have a model class with 6 fields(all with required field validation). I have two views which are using this model. I have three fields in one view(lets say abc.cshtml) and all six in another(lets say xyz.cshtml) The problem is,when I am submitting the form in abc.cshtml and checking ModelState.IsValid property in Controller,it is validating all fields even which are not presented on view,so this property is appears to be false. This is my model

  public class UserModel
  {
        [Required(ErrorMessage = "Enter UserName")]
        public string UserName { get; set; }

        [Required(ErrorMessage = "Enter Password")]
        public string Password { get; set; }

        [Required(ErrorMessage = "Enter Firstname")]
        public string Firstname { get; set; }

        [Required(ErrorMessage = "Enter LastName")]
        public string LastName { get; set; }

        [Required(ErrorMessage = "Enter Contact")]
        public string Contact { get; set; }

        [Required(ErrorMessage = "Enter Address")]
        public string Address { get; set; }
  }

I have only Username, Firstname and Lastname in one view and all six in another. How can I tackle this issue?

Oluwafemi
  • 14,243
  • 11
  • 43
  • 59
  • Create 2 separate view models (you should not be using your data model in a view using for editing) –  Jul 28 '16 at 11:03
  • Have you read this [post](http://stackoverflow.com/questions/10983118/mvc-3-razor-partial-validation-of-model) ? I'm not sure if it is the same as what you are trying to achieve here but i do believe it brings some relevant information – Armando Bracho Jul 28 '16 at 11:10

2 Answers2

3
public class BaseModel{
       [Required(ErrorMessage = "Enter Firstname")]
        public string Firstname { get; set; }

        [Required(ErrorMessage = "Enter LastName")]
        public string LastName { get; set; }

        [Required(ErrorMessage = "Enter UserName")]
        public string UserName { get; set; }


}

   public class UserModel : BaseModel
  {

        [Required(ErrorMessage = "Enter Password")]
        public string Password { get; set; }

        [Required(ErrorMessage = "Enter Contact")]
        public string Contact { get; set; }

        [Required(ErrorMessage = "Enter Address")]
        public string Address { get; set; }
  }

Hope it will help you to reduce redundancy and work with different view also , i think that is he best way to tackle this kind of problem Thanks
Pankaj Gupta
  • 378
  • 2
  • 10
0

In case of submission of 3 properties, before validate model state, have clear error for other properties.

You can remove model errors by doing something like this:

if (ModelState.ContainsKey("{key}"))
    ModelState["{key}"].Errors.Clear();

Key: property Name

In your case there are 3 properties so have to use it (if condition) three time or directly can clear error for known properties.

and after it use

ModelState.IsValid
Arun Prasad E S
  • 9,489
  • 8
  • 74
  • 87
Manveer Singh
  • 351
  • 6
  • 27