有include_once
。我希望能够用asp.net的@Html.Partial
做同样的事情。是否有内置功能来执行此操作?
答案 0 :(得分:1)
我认为ASP.NET中没有这样的功能,但您可以编写自己的功能:
public static class HtmlPartialHelper
{
public static MvcHtmlString PartialOnce(this HtmlHelper htmlHelper, string partialViewName)
{
if (!htmlHelper.ViewContext.RequestContext.HttpContext.Items.Contains(partialViewName))
{
htmlHelper.ViewContext.RequestContext.HttpContext.Items.Add(partialViewName, ""); //any value is good...
return htmlHelper.Partial(partialViewName);
}
return null; //May be string.Empty is better
}
public static bool RenderPartialOnce(this HtmlHelper htmlHelper, string partialViewName)
{
if (!htmlHelper.ViewContext.RequestContext.HttpContext.Items.Contains(partialViewName))
{
htmlHelper.ViewContext.RequestContext.HttpContext.Items.Add(partialViewName, "");
htmlHelper.RenderPartial(partialViewName);
return false;
}
return true;
}
}
我不是PHP专家,但我希望得到正确的回报......