views:

37

answers:

4

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.

A: 

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
+1  A: 

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
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
I do like that. I think I will start adding more ViewModels into my project.
Lloyd
A: 

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
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
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
That's the way to do it. :)
Robaticus
yeah both same thoughts ... =)
server info
+1  A: 

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
looks a bit like mine :)
Robaticus