Lost of validationsummary when doing RedirectToAction

2

Good day.

my question is this, what happens is that I have a model like this:

  public class RegistroUsuarioDto
{
    public int Id { get; set; }
    [Required(ErrorMessage = "Este campo no puede estar vacio!!!")]
    [Display(Name ="No. Documento")]
    public int Num_Documento { get; set; }
    [Required(ErrorMessage = "Este campo no puede estar vacio!!!")]
    public string Nombres { get; set; }
    [Required]
    public string Apellidos { get; set; }
    [Required(ErrorMessage = "Este campo no puede estar vacio!!!")]
    [Display(Name ="Correo Personal")]
    public string Email { get; set; }
    [Required(ErrorMessage = "Este campo no puede estar vacio!!!")]
    [DataType(DataType.Password)]
    [Display(Name ="Contraseña")]
    public string Password { get; set; }
    [Required(ErrorMessage ="Este campo no puede estar vacio!!!")]
    [Display(Name ="Confirmar Contraseña")]
    [DataType(DataType.Password)]
    [Compare("Password")]
    public string ConfirmPass { get; set; }

    public string Cargo { get; set; }

    public string Salario { get; set; }

}

and in my Action Index controller this:

  public ActionResult Index()
    {
        var comboContratos = new List<ComboTiposContratos>();
        var comboAreas = new List<Areas>();
        var comboDocumentos = new List<Tipos_documento>();
        comboContratos = rudao.CargaComboContratos();
        comboAreas = rudao.CargaComboAreas();
        comboDocumentos = rudao.CargaComboDocumentos();
        var liscontratos = new SelectList(comboContratos, "Id", "Descripcion");
        var lisareas = new SelectList(comboAreas, "Id", "descripcion");
        var lisdocu = new SelectList(comboDocumentos, "Id", "Descripcion");
        ViewData["combocontratos"] = liscontratos;
        ViewData["comboareas"] = lisareas;
        ViewData["combodocu"] = lisdocu;
        return View();
    }

I have another action with the [httppost] that receives my data from the form the question is that when my model is not valid it does not show me the messages since I have to do a redirecaction something like this:

    [HttpPost]
     public ActionResult Registro(RegistroUsuarioDto rudto,  string  FechaIngreso, string FechaFin, int combocontratos = 0, int comboareas = 0, int combodocu = 0)
    {

        if (ModelState.IsValid)
        {

         int resultado = rudao.InsertUsuario(rudto, combocontratos, comboareas, FechaIngreso, FechaFin,combodocu);
            if(resultado == 0)
            {
                return RedirectToAction("ViewsuccesUsuario", "HojaVidaDto");
            }
            else
            {
                ModelState.AddModelError("Error", "No se ha registrado el usuario revisar!!!");
                return View("Index");
            }
        }
        else
        {

            ModelState.AddModelError("Error", "No se ha registrado el usuario revisar!!!");
            return RedirectToAction("Index");
        }

    }

The point is that I can not do a return view since my SelectList would be empty and generate an error. So how should I do to keep my validationSummary messages in view? and in general how should one handle these cases so as not to lose information?

    
asked by usernovell 25.09.2016 в 07:15
source

1 answer

1

Let's see, there are a couple of things to change so that the ModelState can work.

  
  • You must send the model to the view when it is invalid, so that the form can show the values that the user had   entered.

  •   
  • In the case of your view that requires combos, you must reload the data so I suggest you refactor it in   one method.

  •   
  • It would be advisable that you refactor your model and include the Income Date, comboareas, etc. in RegistroUsuarioDto to have a   more cohesive model and be able to facilitate showing the values   previously selected to the user when this does not pass the process of   validation.

  •   
       [HttpPost]
         public ActionResult Registro(RegistroUsuarioDto rudto)
        {
    
            if (ModelState.IsValid)
            {
    
                int resultado = rudao.InsertUsuario(rudto,rudto.combocontratos,rudto.comboareas,rudto.FechaIngreso,rudto.FechaFin,rudto.combodocu);
                //int resultado = rudao.InsertUsuario(rudto);
                if(resultado == 0)
                {
                    return RedirectToAction("ViewsuccesUsuario", "HojaVidaDto");
                }
                else
                {
                    ModelState.AddModelError("Error", "No se ha registrado el usuario revisar!!!");
                    PrecargarCombos();
                    return View("Index",rudto);
                }
            }
            else
            {           
                ModelState.AddModelError("Error", "No se ha registrado el usuario revisar!!!");
                PrecargarCombos();
                return View("Index",rudto);
            }
        }
    
        private void PrecargarCombos(){
             var comboContratos = new List<ComboTiposContratos>();
             var comboAreas = new List<Areas>();
             var comboDocumentos = new List<Tipos_documento>();
            comboContratos = rudao.CargaComboContratos();
            comboAreas = rudao.CargaComboAreas();
            comboDocumentos = rudao.CargaComboDocumentos();
            var liscontratos = new SelectList(comboContratos, "Id", "Descripcion");
            var lisareas = new SelectList(comboAreas, "Id", "descripcion");
            var lisdocu = new SelectList(comboDocumentos, "Id", "Descripcion");
            ViewData["combocontratos"] = liscontratos;
            ViewData["comboareas"] = lisareas;
            ViewData["combodocu"] = lisdocu;
        }
    
        
    answered by 26.09.2016 в 15:16