This is a migrated thread and some comments may be shown as answers.

Using Razor with OnDocumentReady on content pages

7 Answers 625 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Greg Roberts
Top achievements
Rank 1
Greg Roberts asked on 22 Nov 2010, 05:12 PM
How do we use OnDocumentReady in Razor content pages without it generating multiple "jQuery(document).ready" statements?  To me being able to add these statements in Views and Controls and have them all render at the bottom in one statement is the most important feature, does this not work anymore for Razor? Thanks

7 Answers, 1 is accepted

Sort by
0
Mike Kidder
Top achievements
Rank 1
answered on 24 Nov 2010, 10:44 PM
Greg Roberts,

You can define a "_Layouts.cshtml" file (Razor master page) and place the following towards the bottom of your page:

@Html.Telerik().ScriptRegistrar();

Now in your views, put your "OnDocumentReady" code where it's needed.  

@{
    Html.Telerik().ScriptRegistrar().OnDocumentReady(@"
 
         // put some javascript goodness in here
         var foo = 'bar';
      ");
}

When the page is rendered,  your code blocks, and whatever Telerik MVC controls you have defined in your view are combined into a single jQuery(document).ready(function(){ ... });
NOTE:  If you forget to put the @Html.Telerik().ScriptRegistrar(); into your layout page, your javascript will not output, and any ajax enabled Telerik MVC controls will revert to server events (postback) since its javascript is not rendered either.

UPDATE:  Use @<text> ... </text> in latest Telerik release in place of the string literal @" ... ".   See Cassandra's post below
0
Victor
Top achievements
Rank 1
answered on 20 Dec 2010, 04:04 PM
Hi!

Nice to find a solution to this for Razor. Just one quick question: Is there any way to achieve this without encapsulating it in a string? Before Razor the () =>  { ... }   makes it possible to write basically anything - is there any equivalent in Razor?

Thanks
/Victor
0
Cassandra
Top achievements
Rank 1
answered on 26 Jan 2011, 05:07 PM
I was looking for an answer to the same question and was able to do the following with Razor without resorting to just a string being passed in:
@{
    Html.Telerik().ScriptRegistrar().OnDocumentReady(
    @<text>
        // put some javascript goodness in here
        var foo = 'bar';
    </text>);
}
0
Mike Kidder
Top achievements
Rank 1
answered on 26 Jan 2011, 05:44 PM
Cassandra,

Yes, the Razor <text> code block now works with Telerik MVC controls (Version: 2010.3 1318 (Jan 18, 2011) and later). It coincides with the MVC3 RTM release.   Highly recommended update.
0
Victor
Top achievements
Rank 1
answered on 27 Jan 2011, 09:37 AM
Very nice follow-up, thanks a lot!

/Victor
0
Sydney
Top achievements
Rank 1
answered on 28 Jan 2011, 12:07 AM
What if my "javascript goodness" contains jquery including $(document).ready...?  
0
Phat Nguyen
Top achievements
Rank 1
answered on 26 Feb 2011, 04:52 AM
how to embed server code inside that <text> block? When i run this code :
@{
    Html.Telerik().ScriptRegistrar().OnDocumentReady(
    @<text>
        
        $("#btnSubmit").click(function(){
            if($("#username").val().trim() == "")
            {
                alert("@Resource.GlobalResource.Logon_UsernameRequired");
                return false;
            }
        });
    </text>);
}
It raise error : CS1593: Delegate 'System.Action' does not take 1 arguments
Tags
General Discussions
Asked by
Greg Roberts
Top achievements
Rank 1
Answers by
Mike Kidder
Top achievements
Rank 1
Victor
Top achievements
Rank 1
Cassandra
Top achievements
Rank 1
Sydney
Top achievements
Rank 1
Phat Nguyen
Top achievements
Rank 1
Share this question
or