如何在我的Razor内容/子页面中应用/添加外部或额外的<script>
引用和CSS <link>
?
@model Portal.Common.Models.TempModel
@{
ViewBag.Title = "asdasd";
ViewBag.MissionId = id;
ViewBag.id = 0;
Layout = "~/Views/Shared/_Layout.cshtml";
}
@if (false)
{
<script src="../../Scripts/jquery-1.7.1-vsdoc.js" type="text/javascript"></script>
<link href="../../Content/Site.css" rel="stylesheet" type="text/css" />
<script src="...../ui/jquery.ui.mouse.js"></script>
<script src="...../ui/jquery.ui.draggable.js"></script>
}
<style type="text/css">
img[src*="iws3.png"] {
display: none;
}
@section JavaScript {
<script type="text/javascript" src="https://www.google.com/jsapi"></script>
<script type="text/javascript"></script> "
答案 0 :(得分:6)
在你的_Layout中,你可以有两个部分:一个用于脚本,一个用于样式:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>@ViewBag.Title</title>
@RenderSection("Styles", false)
</script>
</head>
<body>
@RenderBody()
@RenderSection("Scripts", false)
</body>
</html>
然后在您的视图中覆盖您想要的部分:
@model Portal.Common.Models.TempModel
@{
ViewBag.Title = "asdasd";
ViewBag.MissionId = id;
ViewBag.id = 0;
Layout = "~/Views/Shared/_Layout.cshtml";
}
@section Styles {
<!-- main CSS -->
<link href="@Url.Content("~/Content/Site.css)" rel="stylesheet" type="text/css" />
<!-- some external CSS -->
<link href="http://www.example.com/foo.css" rel="stylesheet" type="text/css" />
<!-- some inline CSS -->
<style type="text/css">
img[src*="iws3.png"] {
display: none;
}
</style>
}
@section Scripts {
<script type="text/javascript" src="https://www.google.com/jsapi"></script>
<script src="@Url.Content("~/ui/jquery.ui.mouse.js")></script>
<script src="@Url.Content("~/ui/jquery.ui.draggable.js")></script>
<script type="text/javascript">
// some inline javascript
</script>
}
答案 1 :(得分:0)
另一种方式, 它是有用的,而相同的css和脚本文件是从不同的cshtml文件引用的,并且您没有使用默认布局。 在共享文件夹中创建局部视图(让_Reference.cshtml),并为原始输出添加以下内容:
@Html.Raw("<link href='../../Content/Site.css' rel='stylesheet'>")
.....
@Html.Raw("<script src='../../Scripts/jquery-1.5.1.min.js'></script>")
现在在引用cshtml的head标记中,添加:
<head>
<title>MVC Test</title>
@Html.Partial("_Reference")
</head>
希望这会对某人有所帮助。