开发者

How to Return Helpers Text as data from Action

I have created a following helper (below codes are just to demonstrate what I am looking for)

@helper SayHello(string text)
{
   @text
}

Now from 开发者_运维问答an action i want to return this Helper's Text as Html (or string) as below

public ActionResult MyAction()
{
      //something like this to return just html
      return SayHello("Rusi");
}


I don't know if this is possible, but even if so - PLEASE DO NOT DO THAT! It so much breaks MVC's separation architecture.

Instead, implement the SayHello helper in, say, a .cshtml file which is being called by an action method (you should decorate your action method in a [ChildActionOnly] attribute), and then you invoke the action method from within your Razor pages by using @Html.Action() or @Html.RenderAction().


AFAIK You can not return a simple string as an actionresult method. Instead you should call your function from ajax and handle the returning text via javascript/jquery. Otherwise you can return a view containig your string like a model. Something like that

return View(SayHello("hello world"));
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜