Creating user friendly strings for C# enums


Recently one of my colleague asked me question, he want to create a enum with string values. But C# doesn’t support string based enums, so here is the code snippet which will help you to associate string values to enums. This code is not using any custom attribute, instead it is using DescriptionAttribute class. Here is the enum, with the associated string values.

enum OrderStatus
    [Description("New Order")]
    NewOrder = 1,
    [Description("Order is processing")]
    Processing = 2,
    [Description("Order is shipped")]
    Shipped = 3

And here is the extension method, which returns the string value associated to the enum

public static class Extensions
    public static string Description(this Enum @enum)
        var description = string.Empty;
        var fields = @enum.GetType().GetFields();
        foreach (var field in fields)
            var descriptionAttribute = Attribute.GetCustomAttribute(field,
                typeof(DescriptionAttribute)) as DescriptionAttribute;
            if (descriptionAttribute != null && 
field.Name.Equals(@enum.ToString(), StringComparison.InvariantCultureIgnoreCase))
                description = descriptionAttribute.Description;

        return description;

You can get the description from enum like this.

var orderStatus = OrderStatus.NewOrder;

Happy Programming :)

Update : Based on the comment from NDVictory, source code modified to return the exact value, earlier it was always returning the first value. Thank you NDVictory

MVC5 Ajax form is not updating DIV but replacing the whole page instead


While working on MVC5 application, I faced this issue, I was using Ajax.BeginForm() and from controller, I was returning content result. But instead of updating the DIV, the content was opened in new Tab. Initially I thought it was due to some javascript error, but it was not working with Ajax.ActionLink also. Later I found it was due to missing “Microsoft.jQuery.Unobtrusive.Ajax” package. You can install this via NuGet.

NuGet package - Microsoft.jQuery.Unobtrusive.Ajax

NuGet package – Microsoft.jQuery.Unobtrusive.Ajax

And include it in the BundleConfig.cs.

bundles.Add(new ScriptBundle("~/bundles/jqueryunobtrusive").Include(

And include the jqueryunobtrusive in _layout.cshtml file.


It will resolve this issue.

Happy Programming :)

Load ASP.NET MVC partial views on link click


Some one asked me how we can load partial views on link click. Here is the snippet for the same.

Long version – Using JQuery

<script type="text/javascript">
$("#loadPartialView").click(function () {
    $.get('@Url.Action("LoadPartialView","Home")', {}, function (response) {

And here is the small version using Ajax.ActionLink

@Ajax.ActionLink("Load Partial View", "LoadPartialView", "Home",
    new AjaxOptions() { UpdateTargetId = "Display" })

In the page, you need to add a DIV with Id attribute which got a value “Display”, partial view will be loaded to this DIV.

And here is the controller action

public ActionResult LoadPartialView()
    return PartialView("_PartialView");

Happy Programming :)

HTML5 Application Cache and Azure websites


Today I faced any issue with HTML5 Application Cache (I will post about HTML5 Application Cache later), I deployed an HTML5 application to azure website, but I was getting error like this

Application Cache Error event: Manifest fetch failed (404)

Application Cache Error event: Manifest fetch failed (404)

Application Cache Error event: Manifest fetch failed (404)

This issue was due to the MIME type mapping was missing. Since it is website, developers don’t have direct control over the IIS. Here is the fix, using Web.Config file.

    <remove fileExtension=".appcache" />
    <mimeMap fileExtension=".appcache" 
              mimeType="text/cache-manifest" />

Sometimes we will get this issue while running in IIS Express as well.

Happy Programming :)