gpt4 book ai didi

asp.net-mvc-3 - 将HTML代码附加到现有的Razor部分

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

是否可以将HTML代码附加到现有的 Razor 部分?

以下是我的情况:

我的_layout.cshtml包含以下内容:

@RenderSection("BottomSection", required: false)

在其中一种 View _article.cshtml中,我定义了以下部分:
@section BottomSection
{
<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)
}

并在名为_counter.cshtml的局部 View 中,由上述 View 使用;我想在同一部分(即BottomSection)中添加更多HTML代码。

我尝试在局部 View 中再次声明BottomSection部分:
@section BottomSection{
<text>More data</text>
}

但这没有解决。

有什么方法可以实现-将更多代码动态地附加到MVC 4中已经定义的 Razor 区吗?
请注意,部分 View 不希望来自父 View /模型的任何数据。
我将MVC 4与.Net Framework 4.0/VS2010结合使用。

最佳答案

我不知道如何将内容添加到各个部分(实际上我想知道自己),但是我知道一个技巧可能会产生相似的结果。除了使用部分以外,还可以使用TempData。 TempData与ViewBag非常相似,但是一旦设置了变量,它将一直存在于当前用户那里,直到一个人尝试再次访问它为止(它可以通过对当前用户的几次连续请求而存在,因此建议格外小心)。以下是如何使用它的示例。

在布局中:

@Html.Raw(new MvcHtmlString((string)TempData["BottomSection"]));

在 View 中:
@{
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;
}

在局部 View 中:
@{
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")

关于asp.net-mvc-3 - 将HTML代码附加到现有的Razor部分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12836888/

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