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

在这里找到此代码段,但它给了我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>

最佳答案

在MVC 3中,默认情况下对输出进行编码,因此这应该足够了:

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

关于 Razor 线更换,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/6558642/

10-11 04:29