1

In my view I am looping through my model and building html to display on the page. I am building this html with stringbuilder and if/else conditions.

I have tried to render this string as html but I keep getting stuck.

This is the code:

@model UserManager.Models.vw_UserManager_Model 
<div id="success-message">
    <fieldset>
        <legend><b>@ViewData["Action"]</b> </legend>
        <!-- User created -->
        <div id="content">

            @Html.Raw("some test string");

            @{
                System.Text.StringBuilder sb = new System.Text.StringBuilder();
                int i = 0;
                sb.Append("<ul>");
                foreach (var item in Model)
                {
                    string s = item.ToString();
                    if (s == "," && i != 1)
                    {
                        // When it finds first delimiter, start building the string
                        i = 1;
                        sb.Append("<li>");
                    }

                    else if (s == "," && i == 1)
                    {
                        // When it finds first delimiter, start building the string
                        sb.Append(" : ");
                    }

                    else if (i == 1 && s != "/")
                    {
                        // Contine building the string
                        sb.Append(s);
                    }

                    else if (s == "/")
                    {
                        // End current list item and set i to 0. This will start the next li sequence
                        sb.Append("</li>");
                        i = 0;
                    }

                    else
                    {
                        // Do nothing in the loop   
                    }
                }
                sb.Append("</ul>");
                var str = MvcHtmlString.Create(sb.ToString());

            }

        </div>
    </fieldset>
</div>

Contents of SB

{<ul><li>firstname : sdlmsdm</li><li>lastname : smsd</li><li>salutation : Mr</li><li>password : ksklsd</li><li>email : [email protected]</li><li>userName : sdkmsdmke</li><li>MaxConcurrentUsers : 0</li><li>group_name : [PIAS] UK LTD</li><li>module_name : Connect:Top 1000 Advertisers</li><li>isactive : True</li><li>IsApproved : False</li><li>alf : True</li><li>brad : False</li><li>selected_moduleAlf : Connect:Top 1000 Advertisers : System.String : selected_moduleBrad : false : </ul>}

I can't see anything of the HTML once this code is finished debugging. Does anyone have the answer?

4
  • You mean str is empty after debugging? Commented Apr 9, 2013 at 9:35
  • @slacker str isn't empty because on the breakpoint I have a body of HTML which I built with my loop. Commented Apr 9, 2013 at 9:38
  • Last li tag is not closed Commented Apr 9, 2013 at 10:00
  • @slacker good spot. I will modify my loop. Commented Apr 9, 2013 at 10:03

2 Answers 2

7

It's because you are not writing it. You need to do @Html.Raw(str):

sb.Append("</ul>");
var str = new HtmlString(sb.ToString());

@Html.Raw(str)
Sign up to request clarification or add additional context in comments.

7 Comments

It didn't work. I don't understand why my html isn't rendering. I will edit my post with the new code.
Are you sure you have a good value in your Model. Looking at your code I think you can have this as an example a,b,c,d, right? Why don't you return an "expected valid string" from your controller so you can check that the "view code" is working.
I think the values in the stringbuilder are not valid. I hardcoded a string and that works fine. I will check that again.
I ended up using a string variable instead of a stringbuilder obj and I made sure I closed all my li's and the ul and it has worked. Plus I used @Html.Raw(). Thanks for your help.
Glad it worked. But honestly though, you should NOT have any logic on your views. You should either build the string in your controller or best is have an htmlhelper extension and build your string with it.
|
1

Change your HtmlString to an MvcHtmlString.Create("string") and then just display the created variable.

<div id="content">
    @{
        System.Text.StringBuilder sb = new System.Text.StringBuilder();
        sb.Append("<ul>");
        sb.Append("<li>asdfd</li>");  
        sb.Append("</ul>");
        var str = MvcHtmlString.Create(sb.ToString());

    }

    @str
</div>

1 Comment

There's no need to use MvcHtmlString. You can output properly formatted html to a view using Html.Raw.

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.