45

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

2
  • what's the method declaration look like? Commented Apr 16, 2012 at 17:51
  • HtmlHelper.Hidden(string name, object value) Commented Apr 16, 2012 at 17:53

1 Answer 1

98

Try casting the return value to object:

@Html.Hidden("hiddenDate", (object)ViewBag.myDate.ToString("dd.MM.yyyy"))
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you! That did the trick. I should have thought on this as my first syntax gave me syntax error, but I just didn't cross my mind. Thank you again!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.