gpt4 book ai didi

asp.net-mvc-3 - ASP.NET MVC 3 : Append to sections

转载 作者:行者123 更新时间:2023-12-04 04:41:55 27 4
gpt4 key购买 nike

我想弄清楚是否可以附加到一个部分。这是我的结构:

_Layout.cshtml

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<link href="@Url.Content("~/Content/style.css")" rel="stylesheet" type="text/css" />
@RenderSection("Css", false)
<script type="text/javascript" src="@Url.Content("~/Content/scripts/head.load.min.js")"></script>
</head>
<body class="bg_g">
@RenderBody()
<script type="text/javascript">
@RenderSection("Javascript", false)
</script>
</body>
</html>

登录.cshtml
@{
Layout = "~/Views/Shared/_DMZ.cshtml";
ViewBag.Title = "Logon";
}

@section Javascript += {
// JAVASCRIPT CODE;
}

<div>
Stuff
@{ Html.RenderAction("Register", "Account"); }
@{ Html.RenderAction("Register2", "Account"); }
</div>

注册.cshtml
@{
Layout = null;
}

@section Javascript += {
// More javascript code
}

<div>
Register stuff
</div>

注册2.cshtml
@{
Layout = null;
}

@section Javascript += {
// Even More javascript code
}

<div>
Register stuff part 2
</div>

希望这能解释我真正想做的事情。我也想对我的 css 部分做同样的事情。如果我也可以让它像这样呈现 Javascript,那就更好了:
head.js(
"@Url.Content("~/Content/scripts/jquery-1.6.2.min.js")",
"@Url.Content("~/Content/scripts/jquery.tools.min.js")",
"@Url.Content("~/Content/lib/jquery-validation/jquery.validate.js")",
// Loop through all javascript files included from the sub views and add them just like above
function () {
loginTabs.init();
// Loop through all javascript functions that have been added to the InitFunctions section?
}
)

也许部分不是这个问题的正确解决方案,但我知道必须有一种方法来完成这样的事情。有任何想法吗?

最佳答案

有点晚了 - 但希望对那里的人仍然有用:

我不知道是否有本地方法来实现它,但我已经使用它一段时间了,它真的很有帮助:

public static IHtmlString Resource( this HtmlHelper HtmlHelper, Func<object, HelperResult> Template, string Type )
{
if( HtmlHelper.ViewContext.HttpContext.Items[Type] != null ) ( (List<Func<object, HelperResult>>)HtmlHelper.ViewContext.HttpContext.Items[Type] ).Add( Template );
else HtmlHelper.ViewContext.HttpContext.Items[Type] = new List<Func<object, HelperResult>> { Template };

return new HtmlString( String.Empty );
}

public static IHtmlString RenderResources( this HtmlHelper HtmlHelper, string Type )
{
if( HtmlHelper.ViewContext.HttpContext.Items[Type] == null ) return new HtmlString( String.Empty );

var Resources = (List<Func<object, HelperResult>>)HtmlHelper.ViewContext.HttpContext.Items[Type];

foreach( var Resource in Resources.Where( Resource => Resource != null ) )
{
HtmlHelper.ViewContext.Writer.Write( Resource( null ) );
}

return new HtmlString( String.Empty );
}

用法是:
//This is how you save it
@Html.Resource(@<style>.test{color: #4e4e4e}</style>, "css")

//This is how you read it
@Html.RenderResources("css")

关于asp.net-mvc-3 - ASP.NET MVC 3 : Append to sections,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12307426/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com