Quame's Rampant Rants (QRR)

Icon

QRR

ASP.NET MVC Reaction to Actions

 

If you are familiar with ASP.NET MVC, you are probably familiar with the State Validation mechanism that was shipped starting from v1. Its a great way to validate your model and funnel your validation result to the user if need be. What I find lucky or I should probably say what I thought was lucking was the fact that it only dealt with error. There is nothing wrong with there and it probably the sole intention but I wanted something general. For every event the user executes (action), they expect a reaction. The most common reaction is page reload with content in relation to the user action. With that in mind, I created an Html helper  provide a generic reaction to the user, based on their action. It could be errors, it could be a warning or a successful message. These are thing developers do very often. Below is the code for my Html helper. Please not this is was a quick hack to get it working. I will post my updated to the helper when I make the final changes.

So here is how it works. All my views are strongly typed and accepts a ViewModel type. My ViewModels inherits from a base ViewModel class which provide the implementation for adding ActionMessage’s to the view. As seen below, my ContactViewModel inherits from the ViewModel abstract base class.

 

public enum ActionMessaeType
{
    Success = 1,
    Failure = 2,
    Warning = 3,
    Alert = 4
}

public class ActionMessage
{

    public ActionMessaeType MessageType
    {
        get;
        set;
    }

    public string Message
    {
        get;
        set;
    }

    public string ForAction
    {
        get;
        set;
    }

    public ActionMessage(ActionMessaeType messagetype,
                   string message)
    {
        MessageType = messagetype;
        Message = message;
        ForAction = string.Empty;
    }

    public ActionMessage(ActionMessaeType messagetype, 
                string message, string foraction)
    {
        MessageType = messagetype;
        Message = message;
        ForAction = foraction;
    }
}

public abstract class ViewModel
{
    private List<ActionMessage> _internalactionmessages
                            = new List<ActionMessage>();
    public List<ActionMessage> ActionMessages
    {
        get { return _internalactionmessages; }
    }
    public ViewModel(){}
    public ViewModel Add(ActionMessage actionmessage)
    {
        _internalactionmessages.Add(actionmessage);
        return this;
    }
    public ViewModel Remove(ActionMessage actionmessage)
    {
        _internalactionmessages.Remove(actionmessage);
        return this;
    }
    public ViewModel Clear()
    {
        _internalactionmessages.Clear();
        return this;
    }
}

public class ContactViewModel : ViewModel
{
    public Contact Contact
    {
        get;
        set;
    }
}

 

 

My Contact View take accepts a ContactViewModel. Here is my page tag in my Contact.aspx

<%@ Page Language="C#" MasterPageFile="~/Views/Shared/Site.
Master" Inherits="System.Web.Mvc.ViewPage<YourNameSpace.
Models.ViewModel.ContactViewModel>" %>

Below is the post Contact Action method in my Controller

Code Snippet
  1. [HttpPost] //Post
  2.         public ActionResult Contact([Bind(Exclude = "Id")]Contact contact)
  3.         {
  4.             //if maodel is not valid, send validated contact
  5.             if (!ModelState.IsValid)
  6.             {
  7.                 return View(new ContactViewModel { Contact = new Contact() }
  8.                        .Add(new ActionMessage(ActionMessaeType.Failure, “Please correct the errors in the highlighted below and try again”, “create-contact-failure”)) as ContactViewModel);
  9.             }
  10.  
  11.             //save contact here……
  12.             
  13.             //Clear ModelState or redirect
  14.             ModelState.Clear();
  15.  
  16.             //display view with action message
  17.             return View(new ContactViewModel { Contact = new Contact() }
  18.                         .Add(new ActionMessage(ActionMessaeType.Success, “Thank you for your message. We will get back to you as soon as possible if a response is need.”, “create-contact-success”)) as ContactViewModel);
  19.         }

 

You will notice from the code above that based on the certain events during the action execution, I add an ActionMessage to the ViewModel before its passed on to the view. The ActionMessage take a messagetype (failure, success, alert,…), a message to be displayed and a “foraction” parameter which depicts where the message should show in the view. 

When the  view is rendering, the html helper is called to display the action messages. The helper can take a “foraction” parameter which is used to determine which ActionMessages to show. If no “foraction” is passed, all messages are displayed. Here is the code for the Helper method

Code Snippet
  1. public static class ActionMessageExtension
  2.     {
  3.         public static readonly string ActionMessageCssClassName = “action-message”;
  4.  
  5.         public static string getActionMessageCssClassName(ActionMessaeType messagetype)
  6.         {
  7.             switch (messagetype)
  8.             {
  9.                 case ActionMessaeType.Success: return “action-message-success”;
  10.                 case ActionMessaeType.Alert: return “action-message-alert”;
  11.                 case ActionMessaeType.Failure: return “action-message-failure”;
  12.                 default: return “action-success-warning”;
  13.             }
  14.         }
  15.  
  16.         public static string ActionMessage(this HtmlHelper htmlHelper)
  17.         {
  18.             return ActionMessage(htmlHelper, string.Empty /* message */);
  19.         }
  20.  
  21.         public static string ActionMessage(this HtmlHelper htmlHelper, List<ActionMessage> messages)
  22.         {
  23.             return ActionMessage(htmlHelper, messages, string.Empty /* htmlAttributes */);
  24.         }
  25.  
  26.         public static string ActionMessage(this HtmlHelper htmlHelper, string foraction)
  27.         {
  28.             return ActionMessage(htmlHelper, null /*messages*/, foraction, (object)null /* htmlAttributes */);
  29.         }
  30.  
  31.         public static string ActionMessage(this HtmlHelper htmlHelper, List<ActionMessage> messages,string foraction)
  32.         {
  33.             return ActionMessage(htmlHelper, messages,foraction, (object)null /* htmlAttributes */);
  34.         }
  35.  
  36.         public static string ActionMessage(this HtmlHelper htmlHelper, List<ActionMessage> messages, string foraction, object htmlAttributes)
  37.         {
  38.             return ActionMessage(htmlHelper, messages, foraction, new RouteValueDictionary(htmlAttributes));
  39.         }
  40.         public static string ActionMessage(this HtmlHelper htmlHelper, List<ActionMessage> messages, string foraction, IDictionary<string, object> htmlAttributes)
  41.         {
  42.             //if no messages are passed in, the viewdata model is checked for messages to display
  43.             if (messages == null)
  44.                 messages = (htmlHelper.ViewData.Model as ViewModel).ActionMessages;
  45.             else //merge parameter with view actionmessages
  46.                 messages.AddRange((htmlHelper.ViewData.Model as ViewModel).ActionMessages);
  47.  
  48.             if (messages == null) return null;
  49.             if (!(messages.Count > 0)) return null;
  50.  
  51.             if (foraction != null)
  52.                messages = messages.FindAll(delegate(ActionMessage actmgs) {return actmgs.ForAction == foraction; });
  53.  
  54.             if (messages == null || !(messages.Count > 0)) return null;
  55.  
  56.             StringBuilder result = new StringBuilder();
  57.  
  58.             //Render Success Messages
  59.             RenderActionMessage(messages, ActionMessaeType.Success, foraction, htmlAttributes,ref result);
  60.             //Render Alert Messages
  61.             RenderActionMessage(messages, ActionMessaeType.Failure, foraction, htmlAttributes, ref result);
  62.             //Render Alert Messages
  63.             RenderActionMessage(messages, ActionMessaeType.Alert, foraction, htmlAttributes, ref result);
  64.             //Render Warning Messages
  65.             RenderActionMessage(messages, ActionMessaeType.Warning, foraction, htmlAttributes, ref result);
  66.  
  67.             //Render …..more types of messages
  68.  
  69.             return result.ToString();
  70.         }
  71.  
  72.         private static void RenderActionMessage(List<ActionMessage> messages, ActionMessaeType messagetype, string foraction, IDictionary<string, object> htmlAttributes,ref StringBuilder htmlresults)
  73.         {
  74.             StringBuilder htmlSummary = new StringBuilder();
  75.             
  76.             //Render  Messages
  77.             List<ActionMessage> typemessages = messages.FindAll(delegate(ActionMessage actmgs) { return (actmgs.MessageType == messagetype && actmgs.ForAction == foraction); });
  78.             if (typemessages.Count > 0)
  79.             {
  80.                  
  81.                 TagBuilder messageList = new TagBuilder(“ul”);
  82.                 messageList.MergeAttributes(htmlAttributes);
  83.                 messageList.MergeAttribute(“class”, getActionMessageCssClassName(messagetype));
  84.  
  85.                 foreach (ActionMessage message in typemessages)
  86.                 {
  87.                     if (!String.IsNullOrEmpty(message.Message))
  88.                     {
  89.                         TagBuilder listItem = new TagBuilder(“li”);
  90.                         listItem.SetInnerText(message.Message);
  91.                         listItem.MergeAttribute(“class”, ActionMessageExtension.ActionMessageCssClassName);
  92.                         htmlSummary.AppendLine(listItem.ToString(TagRenderMode.Normal));
  93.                     }
  94.  
  95.                 }
  96.                 messageList.InnerHtml = htmlSummary.ToString();
  97.                 htmlresults.Append(messageList.ToString(TagRenderMode.Normal));
  98.             }
  99.         }
  100.  
  101.  
  102.     }

 

And here is how its used in the Contact.aspx view.

<%=Html.ActionMessage("create-contact-failure") %>

 

I hope this help. I’m having a hard time finding a good code plug in for Live Writer as I am not so pleased with how the code is presented above. When I do find a good one, I will update the code section of the post.

Future

Like i said, this was just a hack. The best way will be to have the Message Collection as a property of the ViewData since it cannot be used in its current state without using a strongly typed view who’s Model inherits from the base ViewModel class. That’s using Injection of some sort to update the ViewData used in the Views. I also want to pipe all the validation errors for the ModelState through the ActionMessage helper so I can use that as an all purpose notification mechanism.

If you have any suggestions, please let me know.

Filed under: .NET

Timestamp issue with MySql connector 6.0 – 6.1 for Entity Framework

This is really a short post as to highlight an issue that I run into (and most will) when using MySql with Entity Framework employing the mysql connector net.

 

Scenario

I work mostly on my laptop which has mysql connector 6.0.4 installed. Using the connector in conjunction with EF v1 generated my domain object with timestamp fields mapping from a binary in my conceptual schema to timestamp in my physical schema.  Everything worked just fine till I deployed to my production machine and realized I didn’t have mysql connector installed on my production server. So…I did what most will do, download the connect but installed the latest version (v6.1). After deploying my app to the server, the unexpected happened. My app was broken. The error message indicated that Edm.binary was not compatible to mysql.timestamp. OK, I was stumped. The app works just fine my my laptop why not on my server.

 

Solution

After a couple of hours using process of elimination, I realized that using version 6.1 of the connector sets your mapping for timestamp from Edm.DateTimeOffset(conceptual schema) to timestamp (physical schema)which worked fine but version 6.0.4 of the connector sets your mapping for timestamp from Edm.binary(conceptual schema) to timestamp (physical schema) which also works as well. DateTimeOffset is a type specific to Sql server so the best option for me was to stay with 6.0.1  (using binary) which is generic and can be used with other db’s if ever need.

Filed under: ASP.NET MVC

Adventures to my dream part 1

I have been hosting most of my personal application from my home pc for a while until charter blocked port 80 and others. This prevented me from doing exactly that. Disappointed in Charter, I switched to  AT&T U-verse for a better service. That worked great for a couple of week and then …..boom, my ports got jammed again. I called U-verse support to help resolve this but they claim they aren’t blocking any ports. Between you and I, I think they are even though DynamicDNS says the ports are open. Long story short, I decided it was time to shell out some cash for a dedicated windows 2008 server, so I did. I’m actually very happy with my server. I configured every aspect of it to the last bit but this blog is not really about how happy i am about my dedicated server but about my experience setting up my applications in IIS 7.

IIS 7 by far is the best IIS release ever. Built ground up from scratch to support mainly the modular architecture requested by so many web administrators. I have been very impressed and at the same time, frustrated. Why frustrated? Well it has its learning curve and without the IIS 7 documentation, I wont be any closer to understanding how IIS 7 works. So I have decided to make a blog post of any knowledge I can pass on to others in relation to configuring IIS

Today’s “Did you know”:  IIS 7 allows you to run multiple sites on the same port each with a different host name. IIS using Host Names to differentiate between different sites and forwards the request to the site. Now, you can host all your sites on port 80 if you wont to so far as their host name are different. Example, say you have 2 sites. www.site1.com and www.site2.com. You can create two sites in IIS with host names “site1.com” and “site2.com” and both binding to port 80. A request to www.site1.com and www.site2.com respectively will be routed to site1.com and site2.com respectively. your request to be served by that sit

Today’s War wound: Setting up ftp in IIS 7 with the new ftp service is very easy and also support the same port binding mechanism explained above. Using the example above, say you extend both site with ftp publishing feature. You will think setting the host names for the ftp binds (same port 21)  should give you the same ability to ftp into www.site1.com and www.site2.com through ftp.site1.com and ftp.site2.com using the host name feature and the same port 21. Well, you can do that but you are in for a treat if you plan on connecting from any ftp client to those ftp site. It just fails. For some reason, the clients don’t quit know who to use host name in their request. The solution is not to use host names for ftp but rather different ports and leaving the host name to unassigned. Then connect to the different ftp sites using the servers ip and the different ports.

I hope this helps anyone in their quest to tame IIS 7.

Filed under: .NET

Spell checking your content

I have been very busy with work since I came back from Ghana so I have had  limited time to blog. But then again, that’s no excuse since I could post a line or two here and there with a hint of wisdom but …. So here I am back at it again. This time, it’s about spelling.  Here is the interesting part, the web is all about content. Anytime you open a browser to a site, you are slapped with many types of contents in different sizes. These include videos, sounds, pictures and lastly and most prominent of them all, text. In our daily work, we are either typing or reading. Both of which deals with text but ask me how many standalone spell checker tools one can employ and you will be amazed at how many there are. Very few I must say. I am still struck by the fact that visual studio does not ship with a spell checker out-of-the-box after 6 versions. Every thing from Windows form design to Web development involves publishing text content of some sort and yet, there isn’t a solid spell checker that comes with it. Instead, we have to resort to MS word , some other text editing tool (Openoffice etc) or plug-in etc. Frankly, every time I run into an interface design software that doesn’t have a spellchecker, I get dumbfounded. Is spell checking really the least most important feature or what. Then again, someone would say, what’s the big deal. If it bothers you so much, why not learn how to spell. Very true; I wont dispute the fact that we do need to know how to spell. After all, its one of the basic medium of communication since the invention of papyrus. But with the volume of content this generation produces daily, most people are bound to make spelling mistakes so why not help avoid it.  Matt Cutts (Head of Google web spam team) points out here as to how important spelling is on the web and how they can turn customers away. People don’t trust website with spelling mistakes. To that end, I know my ranting is not going to change the feature set of content publishing software’s like visual studio etc, so here are a couple of things you can do to keep your customers coming back.

1. Spell check your content in any good text editing tool like MS Office Word

2. Let a colleague or friend prove read your content for you. You can easily miss your mistakes even if you ready it more than you think you should. It came from you and you are bound to miss one or two.

3. If you can find a spellchecker plug-in to your content authoring software, plug it in and use the heck out of it.

4. For web developers, you can find online services that can scrawl your site for spelling mistakes. Beware of the service you choose; as Matt Cutts post suggests that you probably don’t want to use a service who’s own site has spelling mistakes.

I’m actually checking this post for wrong spelling using Windows live writer. Kudos to the windows live team for including a spell checker. With that said,  let me know what you think if you disagree with anything I have said or want to add to it.

Filed under: .NET

Reducing Online Ad Noise

 

THE ROUTING

I have my ritual every morning from the minute I get to work. I get my coffee; next; ask my colleague (he’s an early to rise kind of guy) if there are any issues that needs my attention ASAP. If none; I put my desk in order; spend the next 15 minutes checking emails and catching up with my favorite blogs etc; then to work.  I’m content with all this except for the last 15 min within which I have to check my emails.

 

THE DISLIKE

Because I have come to dislike internet Ad’s with a passion; both at yahoo and hotmail. They just take up space which otherwise can be used by the application. I have read a lot of complain in relation to yahoo and hotmail to get ride of the Ad’s and more but I know this won’t happen. Below is an image from hotmail but mind you, this applies to yahoo and other email client providers as well. Google does a better job at this by making the ad’s section very small and very smart by relating the ad’s to the content of your inbox. 
adbefore

 

THE BECAUSE

Ad’s are not going to go away any time soon.It wont because the advertising business is a cash making machine. USATODAY reported online ad revenue over $21Billion in 2007 and the interesting part is that internet ad’s account for less than 10% of all Ad’s. Meaning there are lots of room for improvement and money to be made by online solutions like yahoo (with yahoo mail, search , …) , Google (mail, search, apps etc), Facebook and more. I can understand why Ad’s are important to these companies because, after all, we don’t pay for the service they provide. We search and check our emails for free for the most part. Unless you have a premium account or something of that sort.  That’s how they make their profit.

 

THE SOLUTION

Gladly, I stumbled on a great Firefox extension called stylish and have to admit, its one of the best extensions around. It allows you to customize the look and feel for any site by saving your modifying style in stylish and it does the rest. The best part of stylish is that it has a huge support and contributing community which makes it easier to find styles for many sites. I wasn’t able to find a one for the new hotmail and live sites so I created one and contributed it to the stylish online community repository. Below is a screen shot after applying stylish. You can apply stylish to any site, any web email client. Now, I can enjoy my routing freely. Take stylish for a spine and you wont regret it.

adafter

Filed under: .NET, CSS, Design, Personal, UX

Del.icio.us