2

我正在使用 MVC3 并想在博客系统中显示 3 行帖子,然后添加一个链接以转到帖子的其余部分,您可以在大多数这样的博客中看到示例

这是我的观点:

@model IEnumerable<Blog.Web.UI.ViewModels.PostViewModel>
@{
    ViewBag.Title = "Index";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<div>

    @foreach (var item in Model)
    {
        <div>
            <h3>
                @Html.ActionLink(item.Title, "Post", "Blog", new { postId = item.Id, postSlug = item.UrlSlug }, null)
            </h3>
        </div>
        <div>
            <span>Category: </span>@item.Category
        </div>
        <div>
            <span>Tag: </span>@item.Tag
        </div>
        <div>
            @item.CreationDate.ToLongDateString()
        </div>

        <div>
            @Html.DisplayTextFor(p => item.Body)
        </div>
    }
</div>

如图所示

   @Html.DisplayTextFor(p => item.Body)

显示整个帖子,但我想做我引用的链接,我认为可以通过 javascript 但我不知道如何!

4

1 回答 1

1

看起来您提供的示例修剪了任何超过给定长度的额外文本。你可以通过像这样修改你的 ViewModel 来做同样的事情:

class PostViewModel 
{
    public string Body {get;set;}
    public string ShortBody 
    {
        get
        {
            return Body.Length <= 140
                ? Body
                : Body.Substring(0, 140) + "...";
        }
    }
}

然后将您的 DisplayTextFor 行更改为:

@Html.DisplayTextFor(p => item.ShortBody)
于 2013-05-17T17:37:11.223 回答