Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 600
Kman Avatar asked Apr 16 '12 17:04

Kman


People also ask

How pass ViewBag value to hidden field in MVC?

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.

What is the use of HTML HiddenFor in MVC?

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.

What is Razor syntax in MVC?

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.


1 Answers

Try casting the return value to object:

@Html.Hidden("hiddenDate", (object)ViewBag.myDate.ToString("dd.MM.yyyy")) 
like image 125
Ropstah Avatar answered Oct 03 '22 11:10

Ropstah