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

1

1 Answer 1

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" }) }
Sign up to request clarification or add additional context in comments.

4 Comments

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.
@MarceloMyara You asking about @helper? Like here stackoverflow.com/questions/10665103/… OR stackoverflow.com/questions/12825699/… ?
This question is (almost) exactly what i intended to do... :D

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.