![](/img/trans.png)
[英]How to prevent Html.RenderAction method to be recognized as html code in javascript?
[英]Html.RenderAction as static method
我最近開始使用MVC4,現在可以看部分視圖。
我目前有一個像這樣的控制器:
public class BlogController : Controller
{
[ChildActionOnly]
public ActionResult MostRecent()
{
...
}
}
然后,使用以下命令行從我的任何視圖中調用它:
@{ Html.RenderAction("MostRecent", "Blog"); }
是否可以做這樣的事情:
public static class PartialHelper
{
public static string RenderMostRecent()
{
return notsurewhat.RenderAction("MostPopular", "Blog");
}
}
因此,在我的代碼中,我需要調用的是:
@PartialHelper.RenderMostRecent()
這樣,我可以隨時更改控制器/動作,而不必在調用該局部視圖的所有地方進行更新。
如果有更簡單的方法可以做到這一點,請公開討論!
謝謝
您可以將其編寫為HtmlHelper
類的擴展方法:
using Sysem.Web.Mvc;
using Sysem.Web.Mvc.Html;
public static class PartialHelper
{
public static void RenderMostRecent(this HtmlHelper html)
{
html.RenderAction("MostPopular", "Blog");
}
}
然后在您的視圖中使用自定義幫助器(在將PartialHelper
靜態類定義在其中的名稱空間帶入視圖的作用域之后):
@{Html.RenderMostRecent();}
您也可以使用Action
方法代替RenderAction
:
public static class PartialHelper
{
public static IHtmlString RenderMostRecent(this HtmlHelper html)
{
return html.Action("MostPopular", "Blog");
}
}
這將允許您在視圖中像這樣調用它:
@Html.RenderMostRecent()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.