1

I'd like to have a list that will shorten a field value if it is too long from a linked Entity Data Model. Something where I could take the following:

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<IEnumerable<MvcDR.Models.DONOR_LIST>>" %>
<asp:Content ID="Content1" ContentPlaceHolderID="MainContent" runat="server">
    <h2>Lists</h2>
    <table>
        <tr>
            <th></th>
            <th>LIST_NAME</th>
            <th>SUMMARY</th>
        </tr>
    <% foreach (var item in Model) { %>
        <tr>
        <td><%: Html.ActionLink("Details", "Society", new { id = item.DONOR_LIST_ID })%> |</td>
        <td><%: item.LIST_NAME %></td>
        <td><%: item.SUMMARY%></td>
    </tr>
<% } %>

</table>

and replace

        <td><%: item.SUMMARY%></td>

with

        <td><%: item.SHORT_SUMMARY%></td>

doing so in Ruby is pretty straight forward, but I am unsure of how to do so working within the Entity data model of asp.net mvc.

Lloyd
  • 1,395
  • 6
  • 20
  • 37
  • DO you mean displaying a shorter version (or cut of the text with some dots ...) of the summary if the summary i.e. is larger then 150 character? – server info Aug 13 '10 at 17:13
  • http://stackoverflow.com/questions/271398/what-are-your-favorite-extension-methods-for-c-codeplex-com-extensionoverflow/1512463#1512463 – Omar Aug 13 '10 at 18:14

4 Answers4

1

I've usually solved this in the past by creating a ViewModel class that represents a view-specific version of some EF model class. You can use something like AutoMapper to help do the "grunt work" of one-to-one field mapping, but then add a calculated SHORT_SUMMARY field of your own.

You then change your view to use the view model:

Inherits="System.Web.Mvc.ViewPage<IEnumerable<MvcDR.Models.DONOR_LIST_VIEW>>"
Matt Peterson
  • 5,169
  • 4
  • 32
  • 34
  • I'm a big fan of the ViewModel approach with MVC. We have an app that we started with a traditional MVC approach, then adopted ViewModels. Things got much simpler for maintenance with the ViewModels. – Robaticus Aug 13 '10 at 17:29
  • I do like that. I think I will start adding more ViewModels into my project. – Lloyd Aug 13 '10 at 18:22
1

You could also do this with an extension method. I'm typing this from scratch, without benefit of an IDE, so please excuse any typos:

public static class Extensions
{
    public static string Shorten(this string str, int maxLen)
    {
       if(str.Length > maxLen)
       {
           return string.Format("{0}...", str.Substring(0, maxlen));
       }

       return str;
    }
}

Then in your asp.net code:

    <td><%: item.SUMMARY.Shorten(100) %></td>
Robaticus
  • 22,857
  • 5
  • 54
  • 63
  • Ohhh, I like this very much. One question, what's the best practice, or accepted convention of where to put this extension method in the project? – Lloyd Aug 13 '10 at 17:48
  • I go back and forth on taking this as the answer, @server_info had the nice addition of the namespace. To follow up my questions, I created a folder called "Helper". I also checked for null and returned null to allow for that. Then on the view page added <%@ Import Namespace="Helpers" %> in the page declaration section. – Lloyd Aug 13 '10 at 18:20
  • yeah both same thoughts ... =) – server info Aug 13 '10 at 19:14
1

I would make a Extension method for string that shortens text... Then you can reuse it on any field...

    namespace Helpers
{
    public static class StringExtensions
    {
        public static string ShortenMyString(this string s, int length)
        {

            // add logic to shorten the string....
        }
    }
server info
  • 1,335
  • 1
  • 14
  • 24
0

Will something like this work?

namespace MvcDR.Models
{
   public partial class DONOR_LIST
   {
      public string SHORT_SUMMARY
      {
         get
         {
            int desiredMaxStringLength = 100;
            return SUMMARY.Substring(0, desiredMaxStringLength) + "...";
         }
      }
   }
}
Shlomo
  • 14,102
  • 3
  • 28
  • 43