0

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");
}
Rusi Nova
  • 2,617
  • 6
  • 32
  • 48

2 Answers2

1

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().

Ofer Zelig
  • 17,068
  • 9
  • 59
  • 93
0

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"));
Iridio
  • 9,213
  • 4
  • 49
  • 71