Ian Brown
Ian Brown

Reputation: 69

Get Razor to not recognize @ symbol every time

So when putting an @ symbol into a javascript portion of an MVC Code such as

$("#filter").tweet({
count: @Model.Tweets,
filter: function(t){ return ! /^@\w+/.test(t["tweet_raw_text"]},
username: '@Model.TwitUser'
});

how do you get around the system thinking that is Razor and the beginning of a code block in the filter: function line?

Upvotes: 0

Views: 128

Answers (3)

Adam Tal
Adam Tal

Reputation: 5961

Escape it using

@@

Or try

@:

Or put the problematic part in

<text></text>

Upvotes: 0

sellmeadog
sellmeadog

Reputation: 7517

You should be able to use the @: syntax for mixing text and Razor variables

$("#filter").tweet(@:{
  count: @Model.Tweets,
  filter: function(t){ return ! /^@\w+/.test(t["tweet_raw_text"]},
  username: '@Model.TwitUser'
});

You may have to play around with the placement of the @: to get it to work, but it should get you the results you're looking for. There are more syntax tips here: http://haacked.com/archive/2011/01/06/razor-syntax-quick-reference.aspx

Upvotes: 1

dotsa
dotsa

Reputation: 941

I assume your JS code is embedded in your view, if then what i usually do is to declare properties on page load and then use them in JS

In View:

<script type="text/javascript">var urlProfile = '@Url.Action("Index", "Profile")';</script>

In my JS separate file:

$("#cancel-step").click(function () { window.location.href = urlProfile; });

Upvotes: 0

Related Questions