2012-05-30 25 views
3

這是我_Layout.cshtmlRenderSection和EditorForModel在asp.net MVC

<html> 
    <head> 
     @RenderSection("Script", false) 
    </head> 
    ... 
</html> 

這是一個簡單的編輯頁面edit.cshtml

@model Shop.Models.Product 
@{ 
    Layout = "~/Views/Shared/_Layout.cshtml"; 
} 
@Html.EditorForModel() 

,這是〜/查看/共享/ EditorTemplates /Product.cshtml

@model Shop.Models.Product 
@section Script { 
    <script>alert("hello");</script> 
} 
... 

@section Script{...}不Product.cshtml工作,因爲EditorForModel的。怎麼辦?

+0

只要把'@section腳本{...}'入'edit.cshtml'和所有其他視圖從您使用'Product.cshtml'。 – nemesv

+0

@nemesv我知道,但我需要一個解決方案中使用'@section腳本{...}'在Product.cshtml –

+0

它不會工作。 '@ section'只能在佈局和常規視圖中使用,所以它在部分視圖,編輯器或顯示模板中不受支持。我想你可以調整你的js的方式,它不會被要求從'Product.cshtml'包括... [編輯器中/顯示模板中使用的部分(指 – nemesv

回答

4

節僅在視圖的工作,而不是在諧音。編輯器模板是一種特殊的部分。無論如何,將javascript放在partial中是不好的做法,所以我只是簡單地在Edit.cshtml視圖中聲明該部分。

但是,如果你非常堅持把你的腳本放在標記的中間,因爲Razor不支持部分中的部分,你可以實現custom helpers來實現它。

相關問題