0

Is it possible to create a HTML.Helper function that renders a Partial View?

In my project, this would allow me to create a Helper for showing information messages (or error messages). This function would (for example) receive the message itself and would render a partial view with the html for showing that message. I know i could just put the html together inside the helper function itself (without using a partial view) but in that case, for changing the looks of the messages shown i would have to mess with code, and not just a partial view file.

Can anyone help me with that one?

Thanks in advance, Marcelo Myara Rio de Janeiro-Brazil

Marcelo Myara
  • 2,841
  • 2
  • 27
  • 36
  • Found it in here: http://stackoverflow.com/questions/2086365/asp-net-mvc-using-render-partial-from-within-an-html-helper :D – Marcelo Myara Oct 28 '12 at 19:30

1 Answers1

2

You can use Html.RenderAction or Html.Action (Difference between Html.RenderAction and Html.Action)

@Html.Action("MyAction", "MyController", new { MyMessage = "test" })

OR

@{ Html.RenderAction("MyAction", "MyController", new { MyMessage = "test" }) }
Community
  • 1
  • 1
webdeveloper
  • 17,174
  • 3
  • 48
  • 47
  • Ok, you're right, but in my case, i want to call it from a user defined helper and not strait from a view... So I would implement another Helper (let's say "@RenderErrorMessage") and from this newly created method i would like to render a partial. – Marcelo Myara Oct 28 '12 at 19:28
  • @MarceloMyara You asking about `@helper`? Like here http://stackoverflow.com/questions/10665103/can-i-have-a-global-razor-helper-outside-of-app-code OR http://stackoverflow.com/questions/12825699/how-to-create-global-helper-functions ? – webdeveloper Oct 28 '12 at 19:56
  • check this: http://stackoverflow.com/questions/2086365/asp-net-mvc-using-render-partial-from-within-an-html-helper – Marcelo Myara Oct 28 '12 at 20:31
  • This question is (almost) exactly what i intended to do... :D – Marcelo Myara Oct 28 '12 at 20:32