Hi im working right now on a page where you input the id of a person, and if its not in the system then it sends you to the Create , so you can register.
However
[HttpPost]
public ActionResult ingresarSolicitante(int? id)
doesn't get the id, even if I change it for a string and a textbox, I cannot get the value. Any ideas?
here is the html
@model Entrega02Programacion03.Models.Solicitante
@{
ViewBag.Title = "ingresarSolicitante";
}
ingresarSolicitante
@using (Html.BeginForm())
{
Buscar Solicitante por celuda: @Html.TextBox("Cedula")
}
and here is the code on the controller
[HttpPost]
public ActionResult ingresarSolicitante(string id)
{
// busco el solicitante
Solicitante solicitante = db.Solicitante.Find(id);
//si exite voy a crear expediente
if (solicitante != null)
{
TempData["solicitante"] = solicitante;
return RedirectToAction("InformeSolicitante");
}
// si no me redirije a crear solicitante
else
{
return RedirectToAction("Create");
}
}
// GET: Solicitantes/Edit/5
public ActionResult Edit(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
Solicitante solicitante = db.Solicitante.Find(id);
if (solicitante == null)
{
return HttpNotFound();
}
return View(solicitante);
}
I dont know whats wrong with the string id on this method that I allways get a null.
Talk1:
cedula != id. Use @Html.TextBox("Cedula", null, new { @id = "Id" }) instead
Talk2:
If your model contains an Id column. Then, you can use @Html.TextBoxFor("Id"). You'll need to clear up the comfusion between the string and int in your parameters. Either, one or the other. You might need to cast it, if necessary. The TextBoxFor, lets you tell it what property you want it to use from your model. When it gets posted the Id is passed in the model.
Solutions1
change textbox name @Html.TextBox("Cedula") to @Html.TextBox("id")
or change the parameter name of your action method to Cedula
Talk1:
Hi don't forget to mark this answer is correct
Solutions2
Your Html Page is PageModelBase and you can get your Id from this. Or in other word put that id into the TextBox like:
@model Entrega02Programacion03.Models.Solicitante
@{
ViewBag.Title = "ingresarSolicitante";
}
ingresarSolicitante
@using (Html.BeginForm())
{
Buscar Solicitante por celuda: @Html.TextBox(Model.Id)
}
If you do this, you can set and get data from HTML and into HTML