ben (aslında ben de bunu bilmek istiyorum) bölümlere şeyler eklemek için nasıl bilmiyorum, ama benzer bir sonuç yaratabilecek bir hile biliyorum. Bölümleri kullanmak yerine TempData'yı kullanabilir. TempData ViewBag gibi bir çoktur, ancak bir değişken ayarlandıktan sonra, geçerli kullanıcı için tekrar erişmeye çalışana kadar geçerli kullanıcı için orada yaşayacaktır (mevcut kullanıcı için birkaç ardışık istekle yaşayabilir, bu nedenle ekstra dikkat önerilir). Aşağıda nasıl kullanılabileceğine dair bir örnek verilmiştir. düzende
: Kısmi görünümünde
@{
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;
}
: görünümünde
@Html.Raw(new MvcHtmlString((string)TempData["BottomSection"]));
@{
var bottomSection = (string)TempData["BottomSection"];
if (bottomSection == null)
{
bottomSection = "";
}
bottomSection += "More data";
TempData["BottomSection"] = bottomSection;
}
Bu da bu sözde bölümleri için bir yardımcı yazarak geliştirilebilir ve/veya bölümlerin içeriğini ayrı bir kısmi hareket ettirerek (aşağıya bakın).
bottomSection += Html.Partial("_StuffToAddToSection").ToString();
Yardımcısı sınıfı:
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);
}
}
Kullanım örneği
düzeni eklenti olarak: görünümünde
@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>);
}
hatta
@TempData.AppendToPseudoSection("BottomSection", Html.Partial("BottomSectionScriptsAndStuff"))
Ve kısmi 10.
@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>)
veya
: bu çözüm neden yanlış olduğunu
@TempData.AppendToPseudoSection("BottomSection", "More data")
söyle lütfen. Yeni oluşturulmuş html'yi zaten oluşturulmuş görünüme eklemek için aynı şeyi denedim – amesh
Yanıtladığınız için teşekkürler, ancak bu yapmak istemedim. Kısmi görünüm daha fazla içerik/komut dosyası referansı vb. Ekleyebilir. Veriler bir gidiş dönüş isteği ekleyecektir ve o zamana kadar sayfam işe yaramayacaktır! – Kay