Set the value of a Html.Hiddenfor

even thought what Raphaël Althaus said is correct using the hard coded string is always a pain during refactoring. so try this

@{
   Model.Car_id = ViewBag.car.id;
}

@Html.HiddenFor(model => model.Car_id)

by this way it will still be part of your model and lot more cleaner.


either Car_id is not a part of your model, then can't use HiddenFor, but have to use Hidden

something like

@Html.Hidden("Car_id", ViewBag.car.id)

assuming you've got something in ViewBag.car.id, the error you get seems to mean that there's nothing in there.

or it's part of your model and you shouldn't need a ViewBag. You should just set its value in the action related to that View.