0

I want to create an object from the class Expediente:

public class Expediente
{
    //Para que sea key y no genere identity la BD 
    [Key]
    public int Codigo { get; set; }

    [Required]
    public Tramite Tramite { get; set; }

    //no le pongo requiered para que no de error al crearlo sin esto
    public DateTime FechaCreacion { get; set; }

    //por defecto hay que ponerlo en true
    public Boolean abierto { get; set; }

    [Required]
    public Funcionario Funcionario { get; set; }

    [Required]
    public Solicitante Solicitante { get; set; }

    public virtual ICollection<EtapaCumplida> etapasCumplidas { get; set; }

}

I can get the Tramite and Solicitante from dropdown list without problem, but the Funcionario has another object inside of it (Grupo), and I don't know how to get it from the database, just from the key from Funcionario:

public class Funcionario
{
        //Indicar que se trata de una dirección de mail para que realice automáticamente la validación
        //Cambiar el nombre que se desplegará en el formulario
        [Key]
        [DisplayName("Email Funcionario")]
        [EmailAddress]
        public string Email { get; set; }

        //Varias validaciones
        [Required(ErrorMessage = "La contraseña no se puede dejar vacía")]
        [DataType(DataType.Password)]
        [MinLength(3, ErrorMessage = "Largo mínimo de la contraseña: 3"),
        MaxLength(12, ErrorMessage = "Largo máximo de la contraseña:12")]
        public string Contrasenia { get; set; }

        [Required(ErrorMessage = "El nombre no se puede dejar vacío")]
        //Cambiar el nombre que se desplegará en el formulario
        [DisplayName("Nombre Funcionario")]
        //Indicar largo maximo y minimo
        [StringLength(20, MinimumLength = 3)]
        public string Nombre { get; set; }

        [Required]
        public Grupo Grupo { get; set; }
}

Any ideas?

this is the code where I populate the drop-down lists

     public ExpedienteCrearViewModel (Expediente expediente , List<Tramite> listTramites, List<Funcionario> listFuncionario)
        {
            this.Expediente = expediente;

            //el primer campo es el valor que va a tomar, y el segundo lo que muestra
            this.Trsmites = new SelectList(listTramites, "Codigo", "Titulo");

            this.Funcionarios = new SelectList(listFuncionario, "Email", "Nombre");
        }

an this is the code on the create method on Expediente

 public ActionResult Create(ExpedienteCrearViewModel vm)
            {

                if (ModelState.IsValid)
                {
                    Expediente ex = new Expediente();

                    ex.Tramite = db.Tramite.Find(vm.idTramite);
                    ex.Solicitante = (Solicitante)Session["solicitante"];
                    ex.abierto = true;
                    ex.FechaCreacion = DateTime.Now;
                    db.Expediente.Add(ex);
                    db.SaveChanges();
                    vm.Trsmites = new SelectList(db.Tramite.ToList(), "Codigo", "Titulo");
                    vm.Funcionarios = new SelectList(db.Funcionario.ToList(), "Email", "Nombre");
                }

                return View(vm);
            }
  • Show the code where you populate the dropdowns. To retrieve children use something like `var funcionarios = context.Funcionarios.Include(f => f.Grupo).ToList();` – Steve Greene Jul 01 '18 at 15:17
  • I will check that, I added on the post the code inside ExpedienteCrearViewModel , where I create the drop-down lists , Funcionario is the one giving me trouble with Grupo inside of it. – neoMetalero Jul 01 '18 at 15:27
  • That is using a select list, so on POST back you will just get the key (EMail). In your HttpPost method you will need to retrieve the Funcionario with its child Grupo using the syntax from my first comment. Also note, if Functionario already exists and has the Grupo and you are just trying to attach a Functionario to an Expediente, there is no reason to worry about Grupo - it comes along automatically. – Steve Greene Jul 01 '18 at 15:39
  • I just added on the post the code on the Create on Expediente , you say that it brings the Grupo already with Funcionario, but when I find Funcionario Grupo is null. What Im missing here? Sorry for all the trouble. – neoMetalero Jul 01 '18 at 16:00
  • Ok, it worked, now I can get the Funcionario with the Grupo, but when I tried to add it to the Expediente I cant do it – neoMetalero Jul 01 '18 at 16:48
  • This is the code im using var funcionario = db.Funcionario.Where(F => F.Email == vm.idFuncionario).Include(F => F.Grupo); ex.Funcionario = funcionario; – neoMetalero Jul 01 '18 at 16:49
  • If you look at the database, Expediente should have a FK into Functionario (Functionario_Id or similar). Likewise, Functionario should have a FK into Grupo (Grupo_Id). So if you are creating a new Expediente and want to attach an existing Functionario (and its related Grupo), all you really need to do is fetch the Functionario. The Grupo will still be related. In fact, you can expose the FK and work directly with those when needed. See [here](https://stackoverflow.com/questions/14915938/in-what-scenarios-do-i-need-foreign-keys-and-navigation-properties-in-entity-fra). – Steve Greene Jul 02 '18 at 13:54

0 Answers0