Tony
Tony

Reputation: 12695

ASP.NET MVC Simple case with JavaScript and Razor

I have a very simple problem - I try to assign a server variable into a javascript variable;

e.g.

 var data = @(DateTime.Now.Day.ToString() + " " + System.Globalization.CultureInfo.GetCultureInfo("pl-PL").DateTimeFormat.GetMonthName(DateTime.Now.Month) + ", " + DateTime.Now.Year.ToString());

 alert(data);

the alert doesn't show, where's the bug ?

The source output is

var data = 4 , ;

alert(data);

but should be 4 december 2011

Upvotes: 0

Views: 110

Answers (1)

StanK
StanK

Reputation: 4770

You need to wrap it in quotes

var data = "@(DateTime.Now.Day.ToString() + " " + System.Globalization.CultureInfo.GetCultureInfo("pl-PL").DateTimeFormat.GetMonthName(DateTime.Now.Month) + ", " + DateTime.Now.Year.ToString())";
alert(data);

Then I get the following alert:

"5 grudzień, 2011"

If you think about it - without quotes wrapped around the string, it is not valid javascript.

Upvotes: 2

Related Questions