开发者

ASP.NET MVC: How to call custom Html Helper from a controller method?

I'm trying to write a controller method that returns an ActionResult.

In this method, i would like to: 1. call an HTML helper method 2. Capture and store the HTML h开发者_开发知识库elper's rendered HTML in a string 3. Return the method with the rendered HTML wrapped as a JSON

How do i call the Html Helper method from my controller method? Simply using the static class HtmlHelper does not work.


var helper = new HtmlHelper<TModel>(new ViewContext(), new ViewPage());

Or, if that doesn't work, you could try this.

Stream filter = Stream.Null;
StreamWriter writer = new StreamWriter(filter);
var viewContext = new ViewContext(this.ControllerContext, 
    new WebFormView("MyView"), 
    new ViewDataDictionary(this.ViewData), 
    new TempDataDictionary(), writer);
var helper = new HtmlHelper<TModel>(viewContext, new ViewPage());

Just replace TModel with the type of your model.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜