0

I have a requirement where I am building a HtmlHelper extension, which will render code articles:

class Article
{
    public string Title { get; set; }
    public string Description { get; set; }
    public string ViewName { get; set; }
}

So far I can get it to render the title and description correctly, but how do I load the HTML from the view into my helper and render that too?

Example of how the helper works:

public static Article(this HtmlHelper helper, Article article)
{
    return $"<h1>{article.Title}</h1>";
    //...need to load/append View HTML as well before returning
}
7
  • HtmlHelper extension methods return MvcHtmlString. Its not clear what your trying to do here. Commented Feb 15, 2016 at 12:32
  • @StephenMuecke inside my partial view, there is some HTML. I want to add this HTML to the article before the article is renderered (so essentially I am writing a wrapper that wraps an article around some HTML in a partial view) - does that help? Commented Feb 15, 2016 at 12:34
  • 1
    @series0ne You should design you Partial View's HTML to receive the Html from the Helper, not the other way. You must set some structure in your html, to expect the html rendered by your helper Commented Feb 15, 2016 at 12:39
  • @PedroBenevides I want it the other way for a reason, that being, that I want to render the HTML in two ways, 1. I want to render the HTML as it is in the view; secondly, I want to render the HTML as a code block on the page, so people can see the HTML code that creates the view that is in the example. Commented Feb 15, 2016 at 12:42
  • 1
    @series0ne just to clarify this for you, this is not partial views... this is HTML MVC helpers, they are complete two different things.... I suggest you change the Title. Commented Feb 15, 2016 at 12:42

1 Answer 1

1

I found the answer myself; In the interest of assisting others:

public static MvcHtmlString Article(this HtmlHelper helper, Article article)
{
    StringBuilder sb = new StringBuilder();
    sb.Append($"<h1>{article.Title}</h1>");
    sb.Append(helper.Partial(article.ViewName).ToHtmlString());
    return new MvcHtmlString(sb.ToString());
}
Sign up to request clarification or add additional context in comments.

3 Comments

and you couldn't include the heading in the Partial? saving you from this...?
It's more complicated that this - this is a very basic example; in actual fact, the whole thing renders the title, a horizontal rule, a description, a panel containing the HTML, a panel footer containing the code that was used for the HTML (the same HTML in the partial view, loaded a different way), and also creates unique ID's against the article for anchor navigation
fair enough... well done on the solution then... I guess was struggling as i didn't know the full intent. I would of nested Partial view but this maybe better for your requirement - Up voted ;-). Don't you need to pass a view model to your Partial

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.