这是我的_Layout.cshtml
<html>
<head>
@RenderSection("Script", false)
</head>
...
</html>这是一个简单的编辑页面edit.cshtml
@model Shop.Models.Product
@{
Layout = "~/Views/Shared/_Layout.cshtml";
}
@Html.EditorForModel()这是~/Views/Shared/EditorTemplates/Product.cshtml
@model Shop.Models.Product
@section Script {
<script>alert("hello");</script>
}
...由于EditorForModel的原因,@section Script{...}在Product.cshtml中不起作用。怎么才能做到呢?
发布于 2012-05-30 14:04:47
截面只能在视图中工作,不能在部分视图中工作。编辑器模板是一种特殊的部分。不管怎样,将javascript放在部分参数中是不好的做法,所以我只需在Edit.cshtml视图中声明该节即可。
但是如果你非常坚持把你的脚本放在你的标记中间,因为Razor不支持部分的部分,你可以实现custom helpers来实现它。
https://stackoverflow.com/questions/10810446
复制相似问题