#.net #entity-framework
Вопрос:
Я новичок в ASP, и я хочу знать, как я могу сохранить текущего пользователя, когда он хочет сделать «Резерв».
Вот AuthController пользователя
public class AuthController : Controller
{
public ActionResult Login()
{
return View();
}
[HttpPost]
public ActionResult Login(Cliente cliente, string ReturnUrl)
{
if (IsValid(cliente))
{
FormsAuthentication.SetAuthCookie(cliente.Rut_Cliente, false);
if(ReturnUrl != null)
{
return Redirect(ReturnUrl);
}
Session["username"] = cliente.Rut_Cliente;
return RedirectToAction("Index", "Home");
}
TempData["mensaje"] = "Nombre o usuario Incorrectos";
return View(cliente);
}
private bool IsValid(Cliente cliente)
{
return cliente.Autenticar();
}
public ActionResult LogOut ()
{
FormsAuthentication.SignOut();
return RedirectToAction("Index", "Home");
}
}
Вот резервный контролер :
public ActionResult Index()
{
ViewBag.reserva = new Reserva().ReadAll();
return View();
}
// GET: Reserva/Details/5
public ActionResult Details(int id)
{
return View();
}
// GET: Reserva/Create
public ActionResult Create()
{
EnviarCliente();
return View();
}
private void EnviarCliente()
{
ViewBag.cliente = new Cliente().ReadAll();
}
// POST: Reserva/Create
[HttpPost]
public ActionResult Create([Bind(Include = "Id, Fecha_Inicio, Fecha_Fin, Estado, Cliente_Id")]Reserva reserva)
{
try
{
// TODO: Add insert logic here
reserva.Save();
TempData["mensaje"] = "Guardado correctamente";
return RedirectToAction("Index");
}
catch
{
return View(reserva);
}
}
If I use a DropDownListFor in the View and select the User it work, but I want to do this with the current user ID who is logged and don’t use the DropDownListFor
Here is the View «Cliente_Id» is ForeignKey
/*@Html.LabelFor(r => r.Cliente_Id, "Cliente")
@Html.DropDownListFor(r => r.Cliente_Id, new SelectList(ViewBag.cliente, "Id", "Nombre"))
<br />*/
@Html.LabelFor(r => r.Fecha_Inicio, "Fecha Inicial")
@Html.TextBoxFor(r => r.Fecha_Inicio, new { type = "date" })
<br />
@Html.LabelFor(r => r.Fecha_Fin, "Fecha Termino")
@Html.TextBoxFor(r => r.Fecha_Fin, new { type = "date" })
I think to use HiddenFor but I don’t know if this really work