Entity Framework : value from textfield in view return null

Multi tool use
Multi tool use


Entity Framework : value from textfield in view return null



I need to filter some list with input from the user. I created the view, and used the columns that it auto-generated. No problem with the column Codigo, but with Cedula, or Email, it doesn't work, and I need to use textboxes which do work.


Buscar expediente Solicitante por celuda: @Html.TextBox("Cedula")<br/>
Buscar expediente funcionario por celuda: @Html.TextBox("Email")<br />



Here is the code of my controller. When I put something in the textfield Codigo it gets the value without problem, but when I do the same on Cedula or Email it only gets null. Any idea whats going on?


[HttpPost]
public ActionResult BuscarExpediente(int? Codigo , int? Cedula, string Email)
{
//Expediente expediente = db.Expediente.Find(id);
//var expediente = db.Expediente.SingleOrDefault(e => e.Codigo == id);
//si me dan el id de expediente lo busco de esta forma
if ((Codigo != null) &&(Cedula == null) &&(Email == null))
{
//var expediente = db.Expediente.Where(E => E.Codigo == Codigo).SingleOrDefault();
//return View(expediente);
TempData["idExpedienteBuscar"] = Codigo;
return RedirectToAction("ExpedientesPorId");
}

//si me dal la cedula de un solicitante busco de esta forma
if((Cedula != null)&& (Email == null)&& (Codigo == null))
{
TempData["ExpedientesCedulaSolicitante"] = Cedula;
return RedirectToAction("ExpedientesPorCedulaSolicitante");
}

//si me dan un funcionario busco de esta forma
if((Email != null)&& (Codigo == null) && (Cedula == null))
{
TempData["ExpedienteBuscarFuncionarioID"] = Email;
return RedirectToAction("ExpedientesPorFuncionario");
}
//si no me dan nada busco de estra otra
else
{
//que de el mensaje de que solo se puede insertar un campo de busqueda
//hay que poner en el view un label o algo para tirar el msj
}

//ViewBag.FechInicio = expediente.FechaCreacion;
//ViewBag.Tramite = expediente.Tramite.Titulo;
//ViewBag.Funcionario = expediente.Funcionario.Nombre;
return View();
}



Here is the code of the view


@model Entrega02Programacion03.Models.Expediente

@{
ViewBag.Title = "BuscarExpediente";
}

<h2>BuscarExpediente</h2>

@using (Html.BeginForm())
{
@Html.AntiForgeryToken()


Expediente




@Html.ValidationSummary(true, "", new { @class = "text-danger" })

@Html.LabelFor(model => model.Codigo, htmlAttributes: new { @class = "control-label col-md-2" })

@Html.EditorFor(model => model.Codigo, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Codigo, "", new { @class = "text-danger" })

</div>
@* Buscar expediente Solicitante por celuda: @Html.TextBox("Cedula")<br/>
Buscar expediente funcionario por celuda: @Html.TextBox("Email")<br />*@

@Html.LabelFor(model => model.Solicitante.Cedula , htmlAttributes: new { @class = "control-label col-md-2" })

@Html.EditorFor(model => model.Solicitante.Cedula, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Solicitante.Cedula, "", new { @class = "text-danger" })

</div>

@Html.LabelFor(model => model.Funcionario.Email, htmlAttributes: new { @class = "control-label col-md-2" })

@Html.EditorFor(model => model.Funcionario.Email, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Funcionario.Email, "", new { @class = "text-danger" })

</div>

@Html.LabelFor(model => model.abierto, htmlAttributes: new { @class = "control-label col-md-2" })

@Html.EditorFor(model => model.abierto, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.abierto, "", new { @class = "text-danger" })

</div>






</div>
</div>

}


@Html.ActionLink("Back to List", "Index")





Can you access the field "abierto" without problem?
– Afonso
Jul 2 at 16:57




1 Answer
1



Seems like the fields that aren't accessible because they aren't on the "root" level - they are available "inside" other models. Try switching your model to:


[HttpPost]
public ActionResult BuscarExpediente(Entrega02Programacion03.Models.Expediente model)
{
int? Codigo = model.Codigo;
int? Cedula = model.Solicitante?.Cedula;
string Email = model.Funcionario?.Email;

//Expediente expediente = db.Expediente.Find(id);
//var expediente = db.Expediente.SingleOrDefault(e => e.Codigo == id);
//si me dan el id de expediente lo busco de esta forma
if ((Codigo != null) &&(Cedula == null) &&(Email == null))
{

//var expediente = db.Expediente.Where(E => E.Codigo == Codigo).SingleOrDefault();
//return View(expediente);
TempData["idExpedienteBuscar"] = Codigo;
return RedirectToAction("ExpedientesPorId");

}
//si me dal la cedula de un solicitante busco de esta forma
if((Cedula != null)&& (Email == null)&& (Codigo == null))
{
TempData["ExpedientesCedulaSolicitante"] = Cedula;
return RedirectToAction("ExpedientesPorCedulaSolicitante");

}
//si me dan un funcionario busco de esta forma
if((Email != null)&& (Codigo == null) && (Cedula == null))
{
TempData["ExpedienteBuscarFuncionarioID"] = Email;
return RedirectToAction("ExpedientesPorFuncionario");
}
//si no me dan nada busco de estra otra
else
{
//que de el mensaje de que solo se puede insertar un campo de busqueda
//hay que poner en el view un label o algo para tirar el msj
}

//ViewBag.FechInicio = expediente.FechaCreacion;
//ViewBag.Tramite = expediente.Tramite.Titulo;
//ViewBag.Funcionario = expediente.Funcionario.Nombre;
return View();
}






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.

aHbI,wRiHxzI ziSxWcA9
hSD9SuUP,1sgdnhjx,Y88PvT8t8,QtsYxyj4co4Tp5aU6ejdAxUO,cQg0PplZj ib PtuVyUKo

Popular posts from this blog

PHP contact form sending but not receiving emails

Do graphics cards have individual ID by which single devices can be distinguished?

Create weekly swift ios local notifications