Kman
Kman

Reputation: 5001

MVC Razor HTML Helper syntax: Viewbag in Html.Hidden object parameter

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

Upvotes: 45

Views: 64366

Answers (1)

Ropstah
Ropstah

Reputation: 17794

Try casting the return value to object:

@Html.Hidden("hiddenDate", (object)ViewBag.myDate.ToString("dd.MM.yyyy"))

Upvotes: 98

Related Questions