Razor String Replace

rackemup420 picture rackemup420 · Jul 2, 2011 · Viewed 30.7k times · Source

I was looking for a way to do some kind of string replace.

Found this snippet on here, but it gives me the error that MvcHtmlString is not defined:

@MvcHtmlString.Create(Html.Encode(comic.name).Replace(" ", "-"));

Whole Section Of Code:

@{
    var comicName = UrlData[0];

    var db = Database.Open("PhotoGallery");
    var comics = db.Query(@"SELECT * FROM Comics WHERE name = @0", comicName).ToList();

    Page.Title = comicName;
}
<div><a href="@Href("~/")">Home</a> -> <a href="@Href("~/Comics")">Comics Categories</a> -> <strong>@comicName</strong></div>   
<div style="clear: both;">&nbsp;</div>
<div class="sidebar">
<center><img src="@Href("~/Images/Comics", comicName + ".jpg")" title="@comicName" width="320" height="498" /><br />
@comics.Count Issue(s)</center>
</div>
<div class="main">
    <h1>@comicName</h1>
@foreach (var comic in comics) { 
    @MvcHtmlString.Create(Html.Encode(comic.name).Replace(" ", "-"));
<ul class="thumbnails gallery">
        <li class="gallery">
            <a href="@Href("~/Comic/View", comic.name + " " + comic.issue)">
                <img title="@comic.name #@comic.issue" src="@Href("~/Images/Comics", comicName + "/" + comic.issue + ".jpg")" class="thumbnail-no-border" width="200" height="200" />
                <span class="below-image">@comic.name #@comic.issue</span>
                <span class="image-overlay"><strong>@comic.pages Pages</strong></span>
            </a>
        </li>
</ul>    
}    
</div>

Answer

Sergi Papaseit picture Sergi Papaseit · Jul 2, 2011

In MVC 3 output is encoded by default, so this should be enough:

@comic.name.Replace(" ", "-")