0

I' creating two MVC C# razor engine views and I have/must to use the same model for the two different views, the problem is that:

in the first view I don´t need to get the info of all of the properties of my model just some of them and also don´t have to display all the properties of the model in this first view.

in the second viewI need to get the info from the properties that were not displayed in the first view in order to send fill all the properties model with info and then submit the model to the database.

The problem is that the model uses dataannotations to validate that the properties has values and in the first view is not a problem but in the second one it always appears the validationmessage when the second view just load for the first time

first form first form with no validation message

second form with validation message enter image description here

and this is the model

 #region Datos Empresa
        private string Empresa;

        [Required(ErrorMessage = "Especificar empresa que asistirá al evento", AllowEmptyStrings = false)]
        [Display(Name = "Empresa")]
        public string empresa
        {
            get { return Empresa; }
            set { Empresa = value; }
        }
        #endregion

        #region Datos Evento de Formación
        private string AccionEvento;
        private string DptoEvento;
        private string MunicipioEvento;
        private string LugarEvento;
        private string DireccionEvento;
        private string FechaEvento;
        private double HorasEvento;


        [Required(ErrorMessage = "Favor especificar Acción Formativa")]
        [Display(Name = "Acción Formativa")]
        public string accionEvento
        {
            get { return AccionEvento; }
            set { AccionEvento = value; }
        }

        [Required(ErrorMessage = "Favor especificar departamento")]
        [Display(Name = "Departamento")]
        public string dptoEvento
        {
            get { return DptoEvento; }
            set { DptoEvento = value; }
        }

        [Required(ErrorMessage = "Favor especificar municipio")]
        [Display(Name = "Municipio")]
        public string municipioEvento
        {
            get { return MunicipioEvento; }
            set { MunicipioEvento = value; }
        }

        [Required(ErrorMessage = "Especificar lugar en que se impartira el evento")]
        [Display(Name = "Lugar")]
        public string lugarEvento
        {
            get { return LugarEvento; }
            set { LugarEvento = value; }
        }

        [Required(ErrorMessage = "Especificar dirección en que se impartira el evento")]
        [Display(Name = "Dirección")]
        public string direccionEvento
        {
            get { return DireccionEvento; }
            set { DireccionEvento = value; }
        }

        [Required(ErrorMessage = "Especificar fecha inicio evento")]
        [Display(Name = "Fecha tentaiva Inicio")]
        public string fechaEvento
        {
            get { return FechaEvento; }
            set { FechaEvento = value; }
        }

        [Required(ErrorMessage = "Especificar total horas evento")]
        [Display(Name = "Total Horas")]
        public double horasEvento
        {
            get { return HorasEvento; }
            set { HorasEvento = value; }
        }

        //[DisplayFormat(DataFormatString = "{0:#.####}")]

        public double PrecioEvento { get; set; }
        public string acme { get; set; }

        #endregion

        #region Datos Empleado
        private string NombreEmpleado;
        private string ApellidoEmpleado;
        private string SexoEmpleado;
        private string CargoEmpleado;
        private string NivelEmpleado;
        private string ISSSEmpleado;
        private string DUIEmpleado;
        private int grupo;

        [Required(ErrorMessage = "Especificar nombres de empleado")]
        [Display(Name = "Nombres")]
        public string nombreEmpleado
        {
            get { return NombreEmpleado; }
            set { NombreEmpleado = value; }
        }

        [Required(ErrorMessage = "Especificar apellidos de empleado")]
        [Display(Name = "Apellidos")]
        public string apellidoEmpleado
        {
            get { return ApellidoEmpleado; }
            set { ApellidoEmpleado = value; }
        }

        [Required(ErrorMessage = "Especificar genero")]
        [Display(Name = "Genero")]
        public string sexoEmpleado
        {
            get { return SexoEmpleado; }
            set { SexoEmpleado = value; }
        }

        [Required(ErrorMessage = "Especificar cargo de empleado")]
        [Display(Name = "Cargo")]
        public string cargoEmpleado
        {
            get { return CargoEmpleado; }
            set { CargoEmpleado = value; }
        }

        [Required(ErrorMessage = "Especificar nivel organizacional")]
        [Display(Name = "Nível Organizacional")]
        public string nivelEmpleado
        {
            get { return NivelEmpleado; }
            set { NivelEmpleado = value; }
        }

        [Required(ErrorMessage = "Especificar ISSS de empleado")]
        [Display(Name = "Numero ISSS")]
        [RegularExpression("([0-9]{9})", ErrorMessage = "formato incorrecto de numero de ISSS")]
        public string IsssEmpleado
        {
            get { return ISSSEmpleado; }
            set { ISSSEmpleado = value; }
        }

        [Required(ErrorMessage = "Especificar DUI de empleado")]
        [Display(Name = "Numero DUI")]
        [RegularExpression("([0-9]{8}-[0-9]{1})", ErrorMessage = "formato incorrecto de numero de DUI")]
        public string DuiEmpleado
        {
            get { return DUIEmpleado; }
            set { DUIEmpleado = value; }
        }

        [Required(ErrorMessage = "especificar Grupo")]
        [Display(Name = "Grupo")]
        [Range(1, 9999, ErrorMessage = "grupo no valido")]
        [RegularExpression(@"^(((\d{1})*))$", ErrorMessage = "Grupo no valido")]
        public int Grupo
        {
            get { return grupo; }
            set { grupo = value; }
        }

        [Required(ErrorMessage = "Especificar numero de ISSS a consultar")]
        [Display(Name = "Numero ISSS")]
        [RegularExpression("([0-9]{9})", ErrorMessage = "formato incorrecto de numero de ISSS")]
        public string ISSSBuscar { get; set; }

        public string DUIME {get; set;}
        public string telefono { get; set; }
        public string fax { get; set; }
        public string correo { get; set; }
        public string responsable { get; set; }
        public decimal aporte { get; set; } 
        public string proveedor { get; set; }
        public string NombreEmpresa { get; set; }
        public string accion { get; set; }
        public string respuesta { get; set; }

        #endregion

the first view submit the model with the due properties filled to teh second view, my question is what I need to in order to not show the validation message in the second view? could you please help

Pablo Tobar
  • 614
  • 2
  • 13
  • 37
  • Your editing data. DO NOT use data models. Create 2 view models for your views. –  May 13 '17 at 03:55
  • thanks, what would be the difference? could you give an example? – Pablo Tobar May 13 '17 at 04:09
  • You will have one view model for the first view containing just the properties you want to display/edit, and a second view model (which could probably inherit from the first). When you submit the second view, you map to to an instance of your data model and save the data model. Refer also [What is ViewModel in MVC?](http://stackoverflow.com/questions/11064316/what-is-viewmodel-in-mvc) –  May 13 '17 at 04:12
  • oh, I also was thinking about to use [HttpGet] and [HttpPost], the first one to receive the model(as you suggested a model with only the properties I want to get) and then display the view and when this second view submits the info the action with the [HttpPost] attribute will recive it...my question before begin: it will helps to not show the validationmessage ? – Pablo Tobar May 13 '17 at 04:26

0 Answers0