4

mvcアプリケーション用に_Layout.cshtmlを定義しました。これを以下に示します。

@inherits System.Web.Mvc.WebViewPage
@using Webdiyer.WebControls.Mvc;

<html xmlns="http://www.w3.org/1999/xhtml">
<head>   
    @RenderSection("HeaderContent", false)
</head>

<body> 
    @RenderBody() 
</body>
</html>

SomePage.cshtmlページには、レイアウトを含め、部分的なレンダリング構造も含めました。これは、_MailForm.cshtmlをこのページにレンダリングするためです。

@{
   View.Title = "page";
   Layout = "~/Views/Shared/_Layout.cshtml";
}

@Html.Partial("_MailForm")

私の_MailForm.cshtmlファイルは次のようになります。

@inherits System.Web.Mvc.WebViewPage<CMS.Models.Mail.MailModel>

@section HeaderContent
{
    <script src="@Url.Content("~/Scripts/mail.js")" type="text/javascript"></script>
}

<form>...</form>

_MailForm.cshtmlで宣言されたHeaderContentセクションは、_Layout.cshtmlからレンダリングされ、mail.jsスクリプトをロードすると想定しています。スクリプトは実際にはロードされていないため、フォームロジックが機能していません。そのHeaderContentセクションを_MailForm.cshtmlからSomePage.cshtmlに移動すると、mvcがスクリプトをロードするため、すべてが機能します。

しかし、_MailForm.cshtmlファイル内からそのスクリプトをロードする方法は?

よろしく

4

1 に答える 1

3

わかりました。 @BuildStartedによって書かれたコメントをここに書き直します。これは実際に私の質問に対する答えです。

"This is actually by design. The sections can only populate the direct parent. (...) the simplest method [to solve the issue] would be to combine all your javascript into a single file. That would decrease pageloads bandwidth and generally make it better for the end user as they have to load a javascript file only once...however that's not the solution you're looking for. Since the SomePage.cshtml file has the partial page hard-coded I would say including your required script in that page is ok for now. Also, it's OK to put scripts in the body tag."

于 2010-11-02T13:01:17.120 に答える