剃刀串替换

时间:2011-07-02 17:54:15

标签: razor str-replace

我一直在寻找一种方法来进行某种字符串替换。

在此处找到此代码段,但它给出了MvcHtmlString is not defined

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

整个代码部分:

@{
    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>

1 个答案:

答案 0 :(得分:22)

在MVC 3输出中默认编码,所以这应该足够了:

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