代码之家  ›  专栏  ›  技术社区  ›  ryan

更好的方法。。?

  •  1
  • ryan  · 技术社区  · 14 年前

    我不认为这应该在我看来,而是由控制器处理。我想这可以在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 &raquo;</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);
        }
    
    1 回复  |  直到 14 年前
        1
  •  3
  •   p.campbell    14 年前

    建议保持存储库的原样。建议你延长你的假期 Spotlight Regex 属性或方法中的逻辑。

    格式很好的描述。这样你就看不到逻辑了。

    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>
    
    推荐文章