开发者

ASP.NET MVC partial rendering issue using Razor

开发者 https://www.devze.com 2023-01-22 18:48 出处:网络
I have _Layout.cshtml defined for my mvc application, which is shown below: @inherits System.Web.Mvc.WebViewPage

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 f开发者_开发问答ile 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


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

0

精彩评论

暂无评论...
验证码 换一张
取 消