Tôi không biết làm thế nào để thêm công cụ để phần (trong thực tế, tôi muốn biết rằng bản thân mình), nhưng tôi biết một mẹo có thể tạo ra kết quả tương tự. Thay vì sử dụng các phần, người ta có thể sử dụng TempData. TempData rất giống với ViewBag, nhưng một khi biến được đặt, nó sẽ tồn tại ở đó cho người dùng hiện tại cho đến khi một người cố gắng truy cập lại nó (nó có thể tồn tại qua một vài yêu cầu liên tiếp cho người dùng hiện tại, vì vậy bạn nên thận trọng hơn). Dưới đây là một ví dụ về cách nó có thể được sử dụng.
Trong bố cục:
@Html.Raw(new MvcHtmlString((string)TempData["BottomSection"]));
Trong giao diện:
@{
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;
}
Trong giao diện phần:
@{
var bottomSection = (string)TempData["BottomSection"];
if (bottomSection == null)
{
bottomSection = "";
}
bottomSection += "More data";
TempData["BottomSection"] = bottomSection;
}
Điều này có thể được cải thiện hơn nữa bằng cách viết một helper cho những bộ phận giả và \ hoặc bằng cách di chuyển nội dung của các phần một phần riêng biệt (xem bên dưới).
bottomSection += Html.Partial("_StuffToAddToSection").ToString();
lớp Helper:
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);
}
}
Sử dụng ví dụ
Trong bố trí thêm:
@TempData.PseudoSection("BottomSection")
Theo quan điểm:
@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>)
hoặc
@{
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>);
}
hoặc thậm chí
@TempData.AppendToPseudoSection("BottomSection", Html.Partial("BottomSectionScriptsAndStuff"))
Và trong phần:
@TempData.AppendToPseudoSection("BottomSection", "More data")
Please Tell tại sao giải pháp này là sai. Tôi đã thử tương tự cho việc thêm html mới được tạo ra vào giao diện đã hiển thị – amesh
Cảm ơn bạn đã trả lời, nhưng đây không phải là tôi muốn làm. Chế độ xem một phần có thể thêm nội dung tham khảo thêm nội dung/tập lệnh, v.v. Bắt dữ liệu sẽ thêm yêu cầu khứ hồi và cho đến lúc đó trang của tôi sẽ không hoạt động! – Kay