0

I upgraded an MVC3 proj to MVC4 and it appears some javascript functions are not being called after I emit some C# in Razor.

<script type="text/javascript">
$(function () {
    var obj = @(Html.Raw(Model.InterestsJson)) ;

    alert('This is never called.');
}); 
</script>

When I extract the emitted string from my Model.interestsJson, javascript functions run normally.

<script type="text/javascript">
$(function () {
    var obj = [{ "Id": 1, "Name": "Sports" }, { "Id": 2, "Name": "Entertainment" }];
    alert('This will now be called'); 
}); 
</script>

What is weird is that this did work up until I upgraded the project to MVC4.

The first thing I did was check for js errors but nothing threw an exception.

Any ideas?

1
  • I just love the it does not work problem description. It's so exhaustive. It provides so much details. It's a love. Please go ahead and read sscce.org and ask a real question because I can put the snippet you have shown in a jsfiddle and prove you that it works. The conclusion is that there is something else in your code that you didn't show that's causing the problem. And by mentioning a problem, what is it? Error message? Something? Did you check the FireBug console? Was there something? Commented Dec 26, 2011 at 22:24

1 Answer 1

1

The @(Html.Raw(Model.InterestsJson)) statement emits invalid JS code, so next statement cannot be parsed. Open page source and check what's wrong with it.

Probably, this should fix the problem.

@{ Html.Raw(Model.InterestsJson); }
Sign up to request clarification or add additional context in comments.

Comments

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.