0

here is my layout page

<!DOCTYPE html>
@using System.Web.Optimization;
<html>
<head>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
<meta name="description" content="Metro, a sleek, intuitive, and powerful framework for faster and easier web development for Windows Metro Style.">
<meta name="keywords" content="HTML, CSS, JS, JavaScript, framework, metro, front-end, frontend, web development">
<meta name="author" content="">
<title></title>


@Scripts.Render("~/bundles/jquery")
@Styles.Render("~/bundles/metro-ui")
<script src="//cdnjs.cloudflare.com/ajax/libs/select2/4.0.0/js/select2.min.js"></script>
@Styles.Render("~/bundles/metro-ui/css")
@Styles.Render("~/bundles/css")

and so on

and here is my RazorView

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

<script src="~/Scripts/jquery-2.1.4.min.js"></script>
<script src="~/Scripts/metro.min.js"></script>
<script   src="//cdnjs.cloudflare.com/ajax/libs/select2/4.0.0/js/select2.min.js"></script>
<script src="~/Scripts/jquery-ui-1.10.4.custom.min.js"></script>
<link href="~/Content/metro.css" rel="stylesheet" />
<script src="~/Scripts/select2.min.js"></script>
<link href="~/Content/css/select2.min.css" rel="stylesheet" />
<link href="~/Content/jquery-ui-1.10.4.custom.min.css" rel="stylesheet" />
<link href="//cdnjs.cloudflare.com/ajax/libs/select2/4.0.0/css/select2.min.css" rel="stylesheet" />
<br /><br />
@using (Html.BeginForm("Search", "Home", FormMethod.Get))
{

}
and so on...

my Razor View is inherited to Layout how can i use these scripts in my Razor view i don't need to use these scripts again in my Razor View please give me some solution...?

1 Answer 1

1

remove all header references from your view unless they are specific to only that view.

Views in MVC aren't complete pages if they include a layout. The layout is the page. There's a @RenderBody in the layout that gets replaced with your view information at render time. All common javascript references should added to the layout using bundles. See the Scripts.Render("~/bundles/jquery") reference in the layout.

And even if you have javascript specific to a single page, it should be added using the scripts render section. Then is gets added at page load and not the view render.

Sign up to request clarification or add additional context in comments.

6 Comments

@fran i dont understand your answer please clearify me..if i remove all references from the view so my javascript will not run..
@fran ive already added scripts in layout @Scripts.Render("~/bundles/jquery") @Styles.Render("~/bundles/metro-ui") <script src="//cdnjs.cloudflare.com/ajax/libs/select2/4.0.0/js/select2.min.js"></script> @Styles.Render("~/bundles/metro-ui/css") @Styles.Render("~/bundles/css") and in my view i have include layout page now if i remove javascripts from VIEW my page is not working..thats why i have added in View also..
can you define not working? when you look at the page in the browser when you F12 do you see all the script files getting loaded?
yes it is not showing any error,i meant to say that when i remove scripts from my view in the navigation bar i have dropdownmenu when i click to dropdownmenu it is not working but when i include metro.js in file view then it will work fine.. but i have already include this file in bundle in layout page so when it is not inherriting?
possibly the bundle is cached? Without seeing everything you've don't I can't say.
|

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.