2016-04-06 98 views

回答

0

我得到了解決方案,您可能不想在每個頁面上呈現所有內容。切片讓你控制。

_Layout .cshtml定義腳本標籤這樣的,

<script src="/scripts/libs/jquery-ui-1.10.3.min.js"></script> 
<script src="~/Scripts/jquery.validate.min.js"></script> 
<script src="~/Scripts/jquery.validate.unobtrusive.js"></script> 
@if (IsSectionDefined("bottomScripts")) 
{ 
    @RenderSection("bottomScripts", required: false) 
} 

YourView.cshtml定義截面像這樣

@section bottomScripts{ 
<script src="~/Scripts/jquery.unobtrusive-ajax.min.js"></script> 
} 

這裏是佈局和視圖的MVC標準結構。

_Layout.cshtml

<!DOCTYPE html> 

<html lang="en-us"> 
<head> 
    <meta charset=" utf-8"> 
    <title>@ViewBag.Title</title> 
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no"> 

    <link rel="stylesheet" href="~/content/css/stye1.css" /> 
    <link rel="stylesheet" href="~/content/css/style2.css" /> 
    @if (IsSectionDefined("myStyles")) 
    { 
     @RenderSection("myStyles", required: false); 
    } 
</head> 
<body> 
<header> 
    <a href="/Index"> 
     <h1>My Website<small>Your Slogan</small></h1> 
    </a> 
</header> 
<div> 
    @RenderBody() 
</div> 
<script src="~/Scripts/script1.js"></script> 
<script src="~/Scripts/script2.js"></script> 
@if (IsSectionDefined("myScripts")) 
{ 
    @RenderSection("myScripts", required: false); 
} 
</body> 
</html> 

View1.cshtml

<div>This is View1</div> 
@section myStyles{ 
<link rel="stylesheet" href="~/Pagespecificstyle.css" /> 
} 
@section myScripts{ 
<script src="~/PageSpecificScript.js"></script> 
} 

View2.cshtml

<div>This is View2</div> 
@section myStyles{ 
<link rel="stylesheet" href="~/Pagespecificstyle2.css" /> 
} 
@section myScripts{ 
<script src="~/PageSpecificScript2.js"></script> 
}