Raul Mercado
Raul Mercado

Reputation: 324

ModelState.IsValid is validating referenced entity

I have the following entity:

public class Ambiente
{
    public int AmbienteId { get; set; }

    [Display(Name ="Codigo del Ambiente")]
    [StringLength(10, ErrorMessage ="El ancho máximo es de 10 caracteres")]
    public string Codigo { get; set; }

    [Display(Name ="Nombre del Ambiente")]
    [StringLength(50, ErrorMessage ="El ancho máximo es de 50 caracteres")]
    public string Nombre { get; set; }

    [Required(ErrorMessage ="Especifique una etapa")]
    [Display(Name ="Nombre de la Etapa")]
    public Etapa Etapa { get; set; }
}

as you can see, this class has a property which references an entity called Etapa

public class Etapa
{
    public int EtapaId { get; set; }

    [Required(ErrorMessage ="Especifique un nombre de Etapa")]
    [StringLength(20, ErrorMessage = "El ancho maximo es de 20")]
    [Display(Name ="Nombre de la Etapa")]
    public string Nombre { get; set; }
}

This class Etapa has a validation over the property Nombre. Now regarding the class Ambiente, when submitting data in order to insert in the database I use this code in my Insertar Action:

[HttpPost]
public IActionResult Insertar(Ambiente ambiente)
{
    if (ModelState.IsValid)
    {
        try
        {
            _ambienteRepository.Insertar(ambiente);
            return RedirectToAction("Index");
        }
        catch (Exception ex)
        {
            ModelState.AddModelError("", ex.Message);
        }
    }
    var ambienteViewModel = ObtenerAmbienteViewModel(ambiente);
    return View(ambienteViewModel);
}

when this code reaches the ModelState.IsValid sentence it throws false and the following message appears:

"Especifique un nombre de Etapa"

Which is the text of the property Nombre of my Etapa class.

enter image description here

When I quickwatch my variable ambiente from the parameter in my Insertar Action I see the following:

enter image description here

This Etapa entity is used to populate a dropdown list in my view. All the values already exist in the database, so it's not my intention to add records to this entity.

enter image description here

I don't know how to bypass the validation of this referenced entity Etapa.

Upvotes: 0

Views: 496

Answers (1)

Nan Yu
Nan Yu

Reputation: 27538

The normal way is to create a viewmodel , don't include Etapa property . Assign values to Ambiente object on server side before inserting to database.

If you don't want to change anything . Based on your code ,you can ignore the model state error for Etapa by(but is not a good idea) :

ModelState.Remove("Etapa");

Then assign the required Etapa to make sure Nombreis not empty .The code will like :

ModelState.Remove("Etapa");
if (ModelState.IsValid)
{
    var etapa = _context.Etapa.First(a => a.EtapaId == ambiente.Etapa.EtapaId);
    ambiente.Etapa = etapa;
    _context.Add(ambiente);
    await _context.SaveChangesAsync();
    .....
}

Upvotes: 1

Related Questions