I have a string property in my model called extraValue, and in the view i got a ViewBag with a value, and I want to assign the Value in the viewbag to the Model property (extraValue)
I tried:
@{Model.extraValue = ViewBag.id}
and I got a null value in the extraValue, I'm pretty sure that ViewBag.id holds a number.
any idea?
You cannot get the value from the ViewBag when you post it.
ViewBag is desiged to put some useful data and accessible at rendering view only
If you want to access then you need to preserve in your view using some Hidden Field.
@{Model.extraValue = ViewBag.id}
and
@Html.HiddenFor(m=>m.extraValue).
Now you can able to get the extraValue in the controller, since you posting back with the help of HiddenField
public ActionResult Save(Model model)
{
var extra=model.extraValue;// this will bring your viewbag id assigned
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With