我不知道如何将内容附加到部分(实际上我自己也想知道),但我知道一个可能会产生类似结果的技巧。可以使用 TempData 代替使用部分。TempData 很像 ViewBag,但是一旦设置了变量,它就会为当前用户存在,直到有人尝试再次访问它(它可以通过对当前用户的一些连续请求而存在,因此建议格外小心)。下面是一个如何使用它的例子。
在布局中:
@Html.Raw(new MvcHtmlString((string)TempData["BottomSection"]));
在视图中:
@{
var bottomSection = (string)TempData["BottomSection"];
if (bottomSection == null)
{
bottomSection = "";
}
bottomSection += "<script src='~/Scripts/module/article_details.js' type='text/javascript'></script>\n";
bottomSection += "<script src='~/Scripts/module/modal.js' type='text/javascript'></script>\n";
bottomSection += Model.ExtraStuff + "\n";
TempData["BottomSection"] = bottomSection;
}
在局部视图中:
@{
var bottomSection = (string)TempData["BottomSection"];
if (bottomSection == null)
{
bottomSection = "";
}
bottomSection += "More data";
TempData["BottomSection"] = bottomSection;
}
这可以通过为这些伪部分编写帮助程序和\或通过将部分的内容移动到单独的部分来进一步改进(如下所示)。
bottomSection += Html.Partial("_StuffToAddToSection").ToString();
助手类:
public static class PseudoSectionsHelper
{
public static MvcHtmlString AppendToPseudoSection<T>(this TempDataDictionary TempData, string sectionName, T model, Func<T, HelperResult> content, bool addNewLineCharacter = true)
where T : class
{
return AppendToPseudoSection(TempData, sectionName, content(model).ToString(), addNewLineCharacter);
}
public static MvcHtmlString AppendToPseudoSection(this TempDataDictionary TempData, string sectionName, MvcHtmlString content, bool addNewLineCharacter = true)
{
return AppendToPseudoSection(TempData, sectionName, content.ToString(), addNewLineCharacter);
}
public static MvcHtmlString AppendToPseudoSection(this TempDataDictionary TempData, string sectionName, string content, bool addNewLineCharacter = true)
{
var section = (string)TempData[sectionName];
if (section == null)
{
section = "";
}
else if (addNewLineCharacter)
{
section += "\n";
}
section += content;
TempData[sectionName] = section;
// We return empty MvcHtmlString to be able to use this helper inline (without declaring code block @{ some code... } in view)
return new MvcHtmlString("");
}
public static MvcHtmlString PseudoSection(this TempDataDictionary TempData, string sectionName)
{
var section = (string)TempData[sectionName];
return new MvcHtmlString(section);
}
}
使用示例
在布局中添加:
@TempData.PseudoSection("BottomSection")
鉴于:
@TempData.AppendToPseudoSection("BottomSection", Model, @<text>
<script src='~/Scripts/module/article_details.js' type='text/javascript'></script>
<script src='~/Scripts/module/modal.js' type='text/javascript'></script>
@MvcHtmlString.Create(Model.ExtraStuff)
</text>)
或者
@{
TempData.AppendToPseudoSection("BottomSection", Model, @<text>
<script src='~/Scripts/module/article_details.js' type='text/javascript'></script>
<script src='~/Scripts/module/modal.js' type='text/javascript'></script>
@MvcHtmlString.Create(Model.ExtraStuff)
</text>);
}
甚至
@TempData.AppendToPseudoSection("BottomSection", Html.Partial("BottomSectionScriptsAndStuff"))
部分:
@TempData.AppendToPseudoSection("BottomSection", "More data")