Asp.net mvc 我可以通过编程方式在MVC3Razor视图中获取帮助程序列表吗?
正如标题所述。我有一个MVC3Razor视图,我想通过编程实例化它,并获取它的Html助手列表 有人对如何处理这个问题有什么好的建议吗Asp.net mvc 我可以通过编程方式在MVC3Razor视图中获取帮助程序列表吗?,asp.net-mvc,asp.net-mvc-3,razor,Asp.net Mvc,Asp.net Mvc 3,Razor,正如标题所述。我有一个MVC3Razor视图,我想通过编程实例化它,并获取它的Html助手列表 有人对如何处理这个问题有什么好的建议吗 提前感谢我能够通过执行自定义视图结果并从html助手更新模型来实现这一点 行动方法: public ActionResult GetListViewHelpers() { var model = new TestModel(); // has >>> public List<MyHelper> Hel
提前感谢我能够通过执行自定义视图结果并从html助手更新模型来实现这一点 行动方法:
public ActionResult GetListViewHelpers()
{
var model = new TestModel(); // has >>> public List<MyHelper> Helpers { get; set; }
var testContext = new ControllerContext();
testContext.Controller = new MyController();
testContext.HttpContext = HttpContext;
testContext.RouteData.Values["controller"] = "MyController";
testContext.RouteData.Values["action"] = "Index";
var view = new TestViewResult();
view.ViewData.Model = model;
view.ViewName = "TestView"; // i want a list of the helpers from this view
view.ExecuteResult(testContext);
return View(model); // model.Helpers contains a list of helpers within a required view
}
助手:
public static MvcHtmlString AHelper(this HtmlHelper helper, string name)
{
var model = (TestModel)helper.ViewData.Model;
if(model.Helpers == null)
model.Helpers = new List<MyHelper>();
model.Helpers.Add(new MyHelper()
{
Name = name // add all the info i want from the helpers overloads
});
// .... helper logic here
return null;
}
public static MvcHtmlString-AHelper(此HtmlHelper帮助程序,字符串名称)
{
var model=(TestModel)helper.ViewData.model;
if(model.Helpers==null)
model.Helpers=新列表();
model.Helpers.Add(新的MyHelper()
{
Name=Name//从helpers重载中添加我想要的所有信息
});
//…这里是辅助逻辑
返回null;
}
您试过什么吗?我的第一个想法是使用反射,但不确定如何在视图上工作。不,我没有尝试过任何非常详细的东西,因为Yet,你能更详细地描述你的用例吗?您是在谈论开箱即用@Html.XXX helpers,还是在询问如何访问自己的helpers实现?
public static MvcHtmlString AHelper(this HtmlHelper helper, string name)
{
var model = (TestModel)helper.ViewData.Model;
if(model.Helpers == null)
model.Helpers = new List<MyHelper>();
model.Helpers.Add(new MyHelper()
{
Name = name // add all the info i want from the helpers overloads
});
// .... helper logic here
return null;
}