1

I want to render dynamic HTML content into a variable to later pass it to a function. The reason is, that I receive the actual content through another platform and I can print it out using an internal function. This function accepts values to put into placeholders.

Example:

This is my content:
{mytable}
Blah

Now I can set any content on mytable. Good. The content I want to put there is currently the following.

@using System.Data
@model Dictionary<string, DataView>
<table>
    @foreach (var group in Model)
    {
        <tr>
            <th colspan="3">@group.Key</th>
        </tr>

        foreach (DataRowView data in group.Value)
        {
            <tr>
                <td>@data.Row["col1"]</td>
                <td>@data.Row["col2"]</td>
                <td>@data.Row["col3"]</td>
            </tr>
        }
    }
</table>

Well. What is the best way to actually render the above output into a variable? I firstly thought of just appending every HTML line into a string, but it sounds rather inefficient and weak to me.

I'm not an expert in ASP.NET, I come from PHP and I know a bit about output buffers. Is this a possible way? What would you recommend?

2
  • You may want to provide a bit more info about how and where you are getting your html from, just in case someone can recommend a better strategy for what you are attempting to do (e.g. renderpartial etc). In regards to rendering out html into variables, what I like to do for my email templates and for html contained within json responses is to use the RazorEngine library (github.com/Antaris/RazorEngine). This library allows you to make use of the Razor engine itself so that you can do complex template binding. It's great to reuse your skills around Razor. :) Commented May 14, 2014 at 11:44
  • This sounds like a very good idea and I'd like to use it very well, but the contents I receive are stored in an internal SharePoint site where 'HTML noobs' are working to change texts. I tried to change the process of doing this, but they don't want to change their workflow. No chance to do it "the right and nice way". Thanks! Commented May 14, 2014 at 11:46

2 Answers 2

1

You could create a ViewHelper:

@helper RenderTable(Dictionary<string, System.Data.DataView> model)
{
    <table>
        @foreach (var group in Model)
        {
            <tr>
                <th colspan="3">@group.Key</th>
            </tr>

            foreach (System.Data.DataRowView data in group.Value)
            {
                <tr>
                    <td>@data.Row["col1"]</td>
                    <td>@data.Row["col2"]</td>
                    <td>@data.Row["col3"]</td>
                </tr>
            }
        }
    </table>
}

Then call it:

@{
    string output = RenderTable(someData).ToHtmlString();
}
Sign up to request clarification or add additional context in comments.

2 Comments

As well a nice solution and a bit more practicable to me in this situation than the solution from malkam. Thanks!
This assumes you need the output stored in a variable when you're already rendering some View (i.e, you have a ViewContext available).
1

Create strongly typed partial view and add this partial view in actual view

See below link

http://www.codeproject.com/Tips/617361/Partial-View-in-ASP-NET-MVC

1 Comment

That's near to the solution I tried some minutes ago using sections. But RenderSection just rendered the content directly without returning it. Partial Views seem to be a nice solution, thanks!

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.