String Truncate in MVC

I always wanted a string truncate  – i.e.,  if you have a long string, to have “show less” and “show more”.

So I have decided to do it .

1. See in action first:

image

And after click:

image

2. How can you use in your MVC projects:

Add in your project the \Views\Shared\DisplayTemplates\StringTruncate.cshtml file ( in this folder!) and put
@Html.DisplayFor(m => m.String1, “StringTruncate”)

3. Code: It implies only a div and a hidden – and javascript, of course.

@model string

@{
    string id = ViewData.ModelMetadata.PropertyName;
   string textFull = Html.Encode(Model??"");
   string PartialText = textFull;
   string less = "";
   bool full = true;
   if (PartialText.Length > 20)
   {
       full = false;
       PartialText = PartialText.Substring(0, 20) + "...<a href=javascript:showHide_" + id + "(true)>(more)</a>";
   }

   }

<div title="@textFull" id='@("display"+ id)'>
        @Html.Raw(PartialText )

    </div>
    @{if (full) {
          return;
      }
     }

<div  id='@("hid" + id)' style="display:none">
@textFull
<p><a href='javascript:@("showHide_"+ id) (false)'>less</a></p>
</div>
<script type="text/javascript">
function @("showHide_"+ id) (showFull){

$("#@("display"+ id)").toggle(!showFull);

$("#@("hid"+ id)").toggle(showFull);

}
</script>

4. Source code here:

string Truncate

5. TODO ( homework):

Modify the source in order to have one javascript and send the ids of the hidden and of the div to the function.

friday links 30

 

  1. Managed Extensibility Framework – Building Composable Apps in .NET 4 with the Managed Extensibility Framework
  2. EntityFunctions Methods (System.Data.Objects)
  3. ANTS Performance Profiler for profiling your .NET code – Ahead of the game poster
  4. Siri knows which is the best phone ever, and its not the iPhone | WMPoweruser
  5. How to get your app into the Windows Store – Matt Harrington – Site Home – MSDN Blogs
  6. Visual Studio Gallery
  7. In Memoriam: Our Favorite Apps and Services That Have Gone Belly Up (and Their Replacements)
  8. TFS Power Tools 2008 Powershell Snapin won’t run in on 64-bit in Windows 2008 R2?
  9. Programing issues: Performance Counter Setup on Windows Azure
  10. Free E-Books from Microsoft Press | Mike Hacker
  11. Why Nikola Tesla was the greatest geek who ever lived – The Oatmeal
  12. How a Web Design Goes Straight to Hell – The Oatmeal
  13. Knockout 2.1 is out – Knock Me Out
  14. ASP.NET Web API Updates – May 14 – Henrik’s Blog – Site Home – MSDN Blogs
  15. Sample Code – MSDN Examples in C#, VB.NET, C++, JavaScript, F#
  16. Kendo Blog Posts > Hello Services / WebAPI, REST, JSON and AJAX
  17. My First Experience with ASP.NET Web API 101 : Sam Gentile’s Blog
  18. Free .NET decompiler :: JetBrains dotPeek
  19. AJAX File Uploads with jQuery and MVC 3
  20. Optimistic concurrency support in HTTP and WebAPI – part 2 | Tudor Turcu – blog
  21. 15 Things You Should Give Up To Be Happy
  22. Blogging about : Adding Gravatar To An ASP MVC Project
  23. Blogging about : Create a Selfexpanding Textarea using Javascript
  24. Introducing GitHub For Windows
  25. Parse
  26. Additional Considerations When Using ASP.Net MVC Cross-site Request Forgery Prevention – Ed Hintz (MSFT) – Site Home – MSDN Blogs
  27. What The Fuck Is My Career Objective
  28. IIS 7.0 Server-Side : IIS 7.0 Two-Level Authentication with Forms Authentication and Windows Authentication

expand collapse with jquery

If you have a text that you want to hide and show to a click of a hide/show, put this code fast: ( instead of @Url.Content you can use <%=ResolveUrl ) [code lang="HTML"] <a href="javascript:ShowHide('the_id_of_the_element_to_be_show_or_hide', this)"> <img border="0" src='@Url.Content("~/Content/images/show.gif")' title = 'show hide'> </a> <div id='the_id_of_the_element_to_be_show_or_hide' style='display:hidden'> this is hidden content - will be displayed </div> [/code] [code lang="Javascript"] <script type="text/javascript"> var imgShow = '@Url.Content("~/Content/images/show.gif")'; var imgHide = '@Url.Content("~/Content/images/hide.gif")'; function ShowHide(id, img) { var element = $("#" + id); element.toggle('slow', function () { if (element.is(":hidden")) { img.src = imgShow ; } else { img.src = imgHide ; } } ); } </script> [/code]

friday links 28

  1. project.jpg (JPEG Image, 800 × 600 pixels)
  2. Ardalis – Beyond Role Based Authorization in ASPNET MVC
  3. The Service Locator Pattern
  4. Brad Wilson: Task Parallel Library and Servers, Part 1: Introduction
  5. How WebAPI does Parameter Binding – Mike Stall’s .NET Debugging Blog – Site Home – MSDN Blogs
  6. VibrantCode – What’s new in Razor v2
  7. jQuery Menu: Dropdown, iPod Drilldown, and Flyout styles with ARIA Support and ThemeRoller Ready | Filament Group, Inc., Boston, MA
  8. Stephen Forte`s Blog – Communication struggles at a small company
  9. Moment.js – A lightweight javascript date library
  10. leonidas/transparency
  11. Try Transparency
  12. The Big Glossary of Open Source JavaScript and Web Frameworks with Cool Names – Scott Hanselman
  13. Hello World Using JavaScript – Closure Tools — Google Developers
  14. Using Compression on Windows Phone 7.5 – Share-n-dipity – Site Home – TechNet Blogs
  15. Walkthrough: Creating an Asynchronous HTTP Handler
  16. Redesigning my blog for adaptive layout
  17. Demonstrating Responsive Design
  18. ASP.NET MVC: Add HTML5 Microdata to your applications using Metadata Providers – CodeProject®
  19. #Conspiracy – Romania, a surprise
  20. Siderite’s Blog: programming
  21. Decoupling Backbone Apps From WebSockets | ThoughtStream.new :derick_bailey
  22. Html5 Offline Cache Integration with ASP.NET MVC Bundle – Kazi Manzur Rashid
  23. Consultancy and Hiring – Kazi Manzur Rashid
  24. ASP.NET MVC ViewModel usage and pick your best pattern – Kazi Manzur Rashid
  25. WPF RichText Editor – CodeProject®
  26. Be first + Be Smarter + Cheat = ???
  27. XAML to HTML Conversion Demo
  28. 15 Things People Absolutely Hate About Your Website
  29. 10 classic mistakes that plague software development projects | TechRepublic
  30. RSS & Atom MediaTypeFormatter for ASP.NET WebAPI | StrathWeb
  31. Golden Rule – Wikipedia, the free encyclopedia
  32. Web Dev Report – 5 Things Web Developers Need to Know About CSS
  33. Computer Science 101 | Coursera
  34. First round playing with Memcached
  35. Beware of big Task Parallel Library Operations – Ayende @ Rahien
  36. When half a million Americans died and nobody noticed | News | The Week UK
  37. Consuming ASP.NET Web API Web Services with RestSharp In the trenches with .NET by Harvey Kandola
  38. restsharp/RestSharp
  39. HTTP Message Handlers : Official Microsoft Site
  40. So, you you want to write software?: Life Without Objects
  41. How we made our home page load faster – The Official DeskAway Blog
  42. Common.Logging and compatibility with other libraries – CodeProject®
  43. John Sheehan : The API Developer Experience Baseline
  44. JSIL – .NET to JavaScript compiler
  45. Brad Wilson: Task Parallel Library and Servers, Part 4: TaskHelpers
  46. Android fans: pay for your apps, please | PC Pro blog
  47. RIM To Developers: We’ll Make Sure Your App Earns At Least $10K In Its First Year | TechCrunch
  48. firstImpression.js: A micro-library for detecting new visitors – Ravelrumba by Rob Flaherty
  49. How to Market and Promote your Games and Apps Part 1/4 | Ray Wenderlich
  50. How to Market and Promote your Games and Apps Part 2/4 | Ray Wenderlich
  51. Why $7.99 beats $0.99 | Six to Start
  52. Enhanced ObservableCollection with ability to delay or disable notifications – CodeProject®
  53. Injecting method calls to existing methods of an assembly – CodeProject®
  54. Localization in ASP.NET MVC with Griffin.MvcContrib : The Official Microsoft ASP.NET Forums
  55. Managing app lifecycle so your apps feel always alive – Windows 8 app developer blog – Site Home – MSDN Blogs
  56. Creating a great tile experience (part 1) – Windows 8 app developer blog – Site Home – MSDN Blogs
  57. Async/Await FAQ – Parallel Programming with .NET – Site Home – MSDN Blogs
  58. Inbox Zero
  59. A Visual Browser History, from Netscape 4 to Mozilla Firefox – The Andrew Turnbull Mozilla Network
  60. ASP.NET MVC: Resolve or Inject? That’s the Issue…
  61. Why do web sites and software take so long to build? And why is it so hard? at Scott Porad
  62. OrmHate
  63. CQRS
  64. Clarified CQRS
  65. The Floppy Disk means Save, and 14 other old people Icons that don’t make sense anymore – Scott Hanselman
  66. Introducing the App Center – Facebook Developers

Some code

What you do not like about this code ? ( I have found 3 things – how many can you find?)

public static MyNewDocument  OpenDocument(string FileDocPath , out int codeError)
{

    if (string.IsNullOrEmpty(FileDocPath) || !File.Exists(FileDocPath))
    {
            codeError = 1;
            return null;
    }

    MyNewDocument doc = null;
    try
    {
              doc = new MyNewDocument(FileDocPath);
              if (doc == null)
                    throw new Exception();
              codeError = 0;
              return doc;
     }
     catch
    {
              codeError = 2;
              return null;
    }

}

Azure tools

Azure storage Explorer : http://azurestorageexplorer.codeplex.com/  – like in VS , but simpler and cleaner

Windows Azure ASP.NET Providers Sample : http://code.msdn.microsoft.com/windowsazure/Windows-Azure-ASPNET-03d5dc14 – utils for fast membership and roles. Small problem on local.

More samples here: http://code.msdn.microsoft.com/windowsazure/

And that will be all , after reading the documentation and understanding the concepts ( for example,if you understand the session problem in azure, then you will find a Session provider in the samples and use it)

Andrei Ignat weekly software news(mostly .NET)

* indicates required

Please select all the ways you would like to hear from me:

You can unsubscribe at any time by clicking the link in the footer of our emails. For information about our privacy practices, please visit our website.

We use Mailchimp as our marketing platform. By clicking below to subscribe, you acknowledge that your information will be transferred to Mailchimp for processing. Learn more about Mailchimp's privacy practices here.