Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET MVC 4 Dev Preview Razor in Sections Error

Well, I think the ASP.NET MVC team released a pretty significant bug in the developer preview for asp.net mvc 4, or I'm doing something stupid... Here's the issue and steps to reproduce.

  1. Create a new MVC 4 mobile application
  2. create a new section in the layout (ex. @RenderSection("head",false))
  3. in the controller action just throw a Message into the ViewBag
  4. then in a view that uses the main layout, add the following code below.
@section head {
    $(function() { 
        var newVariableName = "@(ViewBag.Message)";
    });
}

You'll notice that the razor parser actually thinks that the section has been completed for the jquery on dom loaded ending brace instead of the section's ending brace. I tried the exact same code in an asp.net MVC 3 application and it worked with no issues.

Has anyone else come across this bug in the ASP.NET MVC 4 Developer Preview?

like image 961
David Manske Avatar asked Dec 17 '11 14:12

David Manske


1 Answers

A quick hack to solving this issue is to use < text > blocks < /text > around the java script. Here's what it might look like until the ASP.NET MVC team resolves this bug.

@{
<text>
    $(function()
    {
        var newVariableName = "@(ViewBag.Message)";
    });
</text>
}
like image 53
David Manske Avatar answered Sep 21 '22 18:09

David Manske