views:

824

answers:

4

Hi,

I've editing this original question as I think I've narrowed down the problem...

I have one view in my site that will not let me put $document.ready within a masterpage contentplaceholder. I've stripped this page to the bare bones and the only thing that is special about it is it has a custom route in global.asax

routes.MapRoute( "Books", "{controller}/{action}/{keywords}/{pageNumber}", new { controller = "Books", action = "SearchResults" } );

Any idea why this custom route would stop $document.ready working correctly when put in a masterpages contentplaceholder zone?

Thanks

Nick

+2  A: 

Your master page (or view page if you're not using master pages) needs to reference jquery. This is included in the latest Beta release of the MVC framework.

Check to make sure you have jQuery included in the tag of your page.

check the syntax as well...

$(document).ready(function() { alert('loaded'); });

these shortened versions also work:

$().ready(function() { alert('loaded'); });
$(function() { alert('loaded'); });
Ben Scheirman
A: 

weird it works when the ready function is in the head of the masterpage, but not when in the contentplaceholder in my View page.

Nick Swan
You're doing something else wrong. Go over your code with a fine-tooth comb.
Will
And stop posting comments as answers. :)
bzlm
+1  A: 

Just stick it somewhere within the content control of your view page in a <script ...> tag.

<asp:Content ID="Content1" ContentPlaceHolderID="MainContentPlaceHolder" runat="server">
    <div class="contentItem">
        <%!-- yadda --%>
    </div>

    <script type="text/javascript">
        $(document).ready(function() {
         // do your worst
        });
    </script>
</asp:Content>

If you have stuff that runs on every page, you can peel that off into a .js file and access it from the master page. But for functions relating to a specific view, this is probably the simplest way to go and easiest to maintain.

Will
+4  A: 

I had the same problem and it turned out that when I used a certain route it changed the perceived file hierarchy of the site such as the ../../Content link for the .js file didn't work any more. I fixed it by changing my jquery script reference to look like this:

<script src="<%= Url.Content("~/Content/jquery-1.2.6.min.js") %>" type="text/javascript"></script>
Simon Steele
I've run into the same thing and I now make it a habit to use "/Content/..." for all references to JS and CSS files... this way I don't need to worry about it when I update my routes.
Andrew Van Slaars