C# Tips N Tricks: Saving Character Literals as String Characters

We have a requirement that when saving a specific type of string to the database it needs to save newline characters as a string (“\n”) not the character literal (‘\n’). It should have been straight forward but I found myself spinning in circles and decided to make a note on what the final code looked like.

CSHTML View:

	@Html.TextArea("Message", Model.Message, new { maxlength = 5000 })

Saving to DB:

      item.Message = model.Message.Replace("\r\n","\\n");

Pulling from DB:

      if (item != null)
      {
        Message = item.Message.ToStringOrEmpty().Replace("\\n", "\r\n");
      }
      if (Message.IsNullOrEmpty())
        Message = "Message goes here.\r\n\r\nSincerely,";

Bootstrap3 Dialog

Bootstrap3dialog

One of the great things about programming in the world today is that so much of the basic functionality I need can be found somewhere on the Web, as long as you know the correct search string.

I spent a good chunk of time looking for a quick and easy way to use Bootstrap modals without having a ton of ugly code on each page that isn’t really necessary.  Especially for modals that are repeated throughout the software.

This time I was looking for a Bootstrap modal that could be easily used for Alert and Confirmation boxes.  I couldn’t find one out there, even though I knew it should exist, so I built my own.  Then I plugged in a different search string and found exactly what I needed.

Bootstrap3 Dialog (Git Hub)is a nice clean simple solution that meets all our needs.  The code is well laid out and I was able to make some minor modifications that met our custom needs.

ReSharper, getting started again

Years ago I used to use ReSharper on a daily basis. Then I left that company and didn’t have easy access to it any more. I missed it. I finally am at a place where I have a copy again. I am in the process of relearning how to use it. Here are a few linking that I found useful:

1. This is ReSharpers PDF file the most used shortcuts
2. Pluralsights “ReSharper Fundamentals” course
3. 24 + 3 ReSharper tips by Rapid Application Development blog
4. A Code Project article with more detailed examples
5. ReSharper documentation of the various functionality

Lazy Programming: Lowercase URLs

Just so everyone knows, when it comes to programming I prefer to be as lazy as possible. This means that when I got my most recent story to render all routes in lowercase I decided fixing the Routing manually was way too much work. I spent some time on Google and discovered that the built-in lowercase functionality that is built into MVC 4 is broken and its a pain to work around. Instead I am adding a nuget package called LowercaseRoutesMVC to the project. It automatically renders routes in lowercase.  Unfortunately my coworker spent a couple hours yesterday working on updating his code to work manually.

Update: Quote from my boss to the coworker who did it manually yesterday, “hard work sometimes pays off tomorrow but procrastination always pays off immediately.”

MVC Tips n Tricks: Using a button like an action link

I had the need to display a button that works like an action link to redirect the user to a page within an Area and didn’t want to wrap the whole thing in a form just to do the redirect.  Here is what I used.  It allows the same styling and behavior as a button but redirects directly like an action link.

<input type="Submit" 
       value="Add Persona" 
       class="btn btn-primary btn-lg" 
       onclick="location.href='@Url.Action("Detail", "Persona", new { area = "ProfileInfo" })'" />

Web Dev Tips n Tricks: Using font awesome as icons to beautify Input boxes

Today I had a requirement that asked for a textbox that looked similar to the following.  Since we don’t currently use BootStrap or anything similar I had to come up with it on my own.  I found the following post that worked perfectly.emailbox

Purpose of this post is to show you how we can use font awesome to beautify a HTML text field or a HTML number box or similar. Its not exactly a background image but our Font awesome icon will be placed on top of our input box so that it act as one.

Read more: http://jaspreetchahal.org/using-font-awesome-as-background-images-to-beautify-input-boxes/#ixzz32HwoWKHw