MVC 3 jquery renderbody <=> Title (_layout)

So this is my problem: I have in my MVC 3 project my layout with all my js script included:

 <link runat="server" rel="shortcut icon" href="@Url.Content("~/Content/favicone/favicon.ico")" type="image/x-icon"/> <link runat="server" rel="icon" href="@Url.Content("~/Content/favicone/favicon.ico")" type="image/ico"/> <script src="@Url.Content("~/Scripts/jquery-1.7.1.min.js")" type="text/javascript"></script> <script src="@Url.Content("~/Scripts/jquery.jcarousel.min.js")" type="text/javascript"></script> <script src="@Url.Content("~/Scripts/jquery.dimension.js")" type="text/javascript"></script> <script src="@Url.Content("~/Scripts/jquery.cookie.js")" type="text/javascript"></script> <script src="@Url.Content("~/Scripts/globalize.js")" type="text/javascript"></script> <script src="@Url.Content("~/Scripts/cultures/globalize.culture." + CurriculumVitae.Helpers.CultureHelper.GetCurrentCulture() + ".js")" type="text/javascript"></script> <script src="@Url.Content("~/Scripts/jquery.unobtrusive-ajax.min.js")" type="text/javascript"></script> <script src="@Url.Content("~/Scripts/jquery-ui-1.8.17.custom.min.js")" type="text/javascript"></script> <script type="text/javascript"> function mycarousel_initCallback(carousel) {....}</script> 

But only my default view can use these scripts when I switch to another view (in renderbody) and I need to use some jquery, I need to run a RE-IMPLEMENT script to make it work:

 @{ViewBag.Title = "Index";} @section header{ <script src="@Url.Content("~/Scripts/jquery-1.7.1.min.js")" type="text/javascript"></script> <script src="@Url.Content("~/Scripts/jquery.unobtrusive-ajax.min.js")" type="text/javascript"></script> <script src="@Url.Content("~/Scripts/jquery-ui-1.8.17.custom.min.js")" type="text/javascript"></script> <script> $.fx.speeds._default = 1000; $(function () { $("#dialog").dialog({ autoOpen: true, show: "blind", hide: "explode" }); $("#opener").click(function () { $("#dialog").dialog("open"); return false; }); }); </script> } <a id="opener" href="javascript:void();">test </a> <div id="dialog" title="Basic modal dialog"> <p>terst nmsdfnms dfds fsd f JQUERYYYYYYYYYYYYYY</p> </div> 

And this is just the beginning ... I have another problem with my Theme / Culture.

The point is in the source code of my web page, I found all the scripts from _layout (and "view"), but without the @section header, even a simple js call will not work if it is used by a script from my header (_layout)

Thank you

+4
source share
2 answers

I found my problem:

  • Never reinstall or override jquery-1.7.1.min.js within the same network as this will create a conflict between each jquery call.

  • One of my scripts using (jcarousel.js) was misconfigured (initCallback parameter).

Thanks to everyone;)

+1
source

Since you are not specifying a layout in your view, you can try two things.

Make sure your default layout is correctly configured within "~Views/_ViewStart.cshtml" :

 @{ Layout = "~/Views/Shared/_Layout.cshtml"; } 

You can explicitly specify the desired layout of your view:

 @{ ViewBag.Title = "Index"; Layout = "~/Views/Shared/_Layout.cshtml"; } 
0
source

Source: https://habr.com/ru/post/1395203/


All Articles