syntax in Razor, which is slightly less clunky than the
<text>
tag if you just have one or two lines of javascript code to add. The following approach would probably be preferable because it reduces the size of the generated HTML.
Your values contain a dot and Razor, in its brightness, interprets that as an
object.property
. Try using Html.Raw()
:<text>addMarker(@Html.Raw(item.Profile.lat), @Html.Raw(item.Profile.lng));</text>
Using Helper :
just wrote this helper function. Put it in
App_Code/JS.cshtml
:@using System.Web.Script.Serialization
@helper Encode(object obj)
{
@(new HtmlString(new JavaScriptSerializer().Serialize(obj)));
}
Then in your example, you can do something like this:
var title = @JS.Encode(Model.Title);
Notice how I don't put quotes around it. If the title already contains quotes, it won't explode. Seems to handle dictionaries and anonymous objects nicely too!
No comments:
Post a Comment