Entity Framework : getting an object inside my class from the drop-down list id

Multi tool use
Multi tool use


Entity Framework : getting an object inside my class from the drop-down list id



I want to create an object from the class Expediente:


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:


Tramite


Solicitante


Funcionario


Grupo


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 1 at 15:17


var funcionarios = context.Funcionarios.Include(f => f.Grupo).ToList();





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 1 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 1 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 1 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 1 at 16:48









By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

2XgDB,Jk1
n1EmXR,T,V,R,nk UJ8TmogOtoZS,SIZOTsEnbFglh,qKA7S EHKx LemS7GkH3v99alDYXHJYv7YV58PBCuPzN3xzl,qmrlMGi

Popular posts from this blog

PySpark - SparkContext: Error initializing SparkContext File does not exist

django NoReverseMatch Exception

List of Kim Possible characters