user1532669
user1532669

Reputation: 2378

How do I build up an html string to display within a div using razor mvc?

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?

Upvotes: 0

Views: 511

Answers (2)

Lzh
Lzh

Reputation: 3635

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>

Upvotes: 0

Tim B James
Tim B James

Reputation: 20364

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.

Upvotes: 2

Related Questions