我不认为这应该在我看来,而是由控制器处理。我想这可以在SQL中完成(可以快速实现丑陋)或在控制器中(我认为这可能是最好的),或者甚至是HTML帮助器。但是,我不知道如何在控制器中解压缩/重新包装我的IQueryable / IEnumberable。我认为给模板设计师他们需要的一切,然后一些是最好的,因此提供完整的描述和摘录(生成)。
赞赏的想法/想法。
<p>
<% var description = Regex.Replace(Regex.Replace(spotlight.Description, "<[^>]*>", string.Empty), "[\0x0020\r\n]+", " ").TrimEnd();
if (description.Length < 297)
{
%> <%= description %> <%
} else { %>
<%= description.Substring(0, 297) + "..." %> <%
}
%> <a href="<%= Url.Action("Details", "Spotlights", new { id=spotlight.SpotlightID}) %>">Read »</a>
</p>
我的存储库:
public IQueryable<Spotlight> FindAllSpotlights()
{
return from spotlight in db.Spotlights
where spotlight.PublishDate <= DateTimeOffset.Now
orderby spotlight.PublishDate descending
select spotlight;
}
我的控制器:
public ActionResult Index()
{
var spotlights = spotlightRepository.FindTopSpotlights().ToList();
return View(spotlights);
}
答案 0 :(得分:3)
建议保持您的存储库不变。建议您扩展Spotlight
类以封装属性或方法中的所有Regex
逻辑。
使用格式良好的描述扩展Spotlight
的行为。这将使逻辑远离你的观点。
public partial class Spotlight
{
public string WellFormattedDescription()
{
//all the logic to return a well formatted Spotlight.Description
string desc = Regex.Replace(Regex.Replace(this.Description, "<[^>]*>",
string.Empty), "[\0x0020\r\n]+", " ")
.TrimEnd();
if (desc.Length < 297)
return desc;
else
return desc.Substring(0, 297) + "...";
}
}
然后您的视图只会调用:
<p>
<%=spotlight.WellFormattedDescription %>
<a href="<%= Url.Action("Details", "Spotlights", new { id=spotlight.SpotlightID}) %>">foo</a>
</p>