0

I'm using razor mvc and I want to build up an html string so that I can display it within a div, in php it would look like this:

$htmlstring = '';
foreach ($item as $items)
{
    $htmlstring .= '<p>'.$item.'</p>';
}

<div>
    <? echo $htmlstring; ?>
</div>

How would I do the equivalent in razor mvc?

2
  • Asp.Net MVC Razor? What language? Commented Oct 22, 2014 at 10:11
  • c# - sorry, new to this. Commented Oct 22, 2014 at 10:14

2 Answers 2

2

You could just stick your code within a foreach loop on the Razor View

<div>
    @foreach ( var item in Model.Items ){
        <p>@item</p>
    }
</div>

Assuming that the Model you pass in has a property called Items which is a Collection of some type.

Sign up to request clarification or add additional context in comments.

Comments

0

I think you mean the ASP .NET MVC Razor View Engine.

@{
    //this should come from controller
    List<string> items = new List<string>() { "one", "two", "three" };
}

<div id="theDiv">
@foreach (var v in items)
{ 
    //no need for building a string, sense we can just output the <p>xyz</p> inside the div
    <p>@v</p>
}
</div>

Comments

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.