4

I have _Layout.cshtml defined for my mvc application, which is shown below:

@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>

On the page SomePage.cshtml, I have included the layout, and also included the partial rendering construction, because I want my _MailForm.cshtml to be rendered on this page:

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

@Html.Partial("_MailForm")

My _MailForm.cshtml file looks following:

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

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

<form>...</form>

The HeaderContent section declared in _MailForm.cshtml, suppose to be rendered from _Layout.cshtml and load mail.js script. The script is actually not loaded and because of that my form logic is not working. If I move that HeaderContent section from _MailForm.cshtml to SomePage.cshtml, everything works because mvc loads the script.

But how to load that script from inside _MailForm.cshtml file?

Regards

3
  • This is actually by design. The sections can only populate the direct parent...I ran into this same issue, as well. Commented Oct 29, 2010 at 22:14
  • Is there any workaround for that, or some different approach to solve the issue ? Moving that section to direct child of its rendering parent is the only solution ? Commented Oct 30, 2010 at 9:44
  • Well, the simplest method 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. Commented Nov 1, 2010 at 17:20

1 Answer 1

3

Ok, I'll rewrite here the comment written by @BuildStarted, which is actually answer for my question:

"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."

Sign up to request clarification or add additional context in comments.

2 Comments

But what about the javascript's related style or stylesheets?, some javascript plugins such as jquery ui stuff, dropdownmenus and others are tightly bound to css and styles which cannot be put directly into the body.
Besides, in most cases you don't have access to the body at all from a partial view. Any solution?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.