I have a view where everything will be populated by the user – but relates to a parent entity. I pass that Id to my view using ViewBag, but I don’t know how to get it back to the post action in the controller. I have tried hidden form fields, but it isn’t showing in the post, or I do not know how to grab it…
Controller:
public ActionResult AddCar(int id)
{
ViewBag.Id = id;
return View();
}
View (tried):
@using (Html.BeginForm("AddReturn", "DealerAdmin", new { id = carId }))
{
View (tried):
@Html.Hidden(carId.ToString())
HOw do I retrieve the value in my post action in my controller? Or is there a better/different way to approach it?
THanks
The hidden field should works. The problem is that your controller did not accept it.
You can use ViewModel to achieve this. Or, use the code below in your action: