I am trying to add a object to my Html.Hidden HTML helper, but I can't get the syntax quite right.
Syntax 1:
@Html.Hidden("hiddenDate", ViewBag.myDate.ToString("dd.MM.yyyy"))
Results in runtime error and it can't resolve the @Html.Hidden in view.
Syntax 2:
@Html.Hidden("hiddenDate", new { String = ViewBag.myDate.ToString("dd.MM.yyyy")})
Sets the value="{ String = 16.04.2012 }"
I would like to get the value to only "16.04.2012", but no success after several syntax tweaks
The ViewBag object value will be set inside Controller and then inside the View, the value will be assigned to the Hidden Field created using Html. Hidden helper function in ASP.Net MVC Razor.
HiddenFor<TModel,TProperty>(HtmlHelper<TModel>, Expression<Func<TModel,TProperty>>, Object) Returns an HTML hidden input element for each property in the object that is represented by the specified expression, using the specified HTML attributes.
Razor is a markup syntax that lets you embed server-based code into web pages using C# and VB.Net. It is not a programming language. It is a server side markup language. Razor has no ties to ASP.NET MVC because Razor is a general-purpose templating engine.
Try casting the return value to object
:
@Html.Hidden("hiddenDate", (object)ViewBag.myDate.ToString("dd.MM.yyyy"))
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