Creating user friendly strings for C# enums

Standard

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;
                break;
            }
        }

        return description;
    }
}

You can get the description from enum like this.

var orderStatus = OrderStatus.NewOrder;
Console.WriteLine(orderStatus.Description());

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

Standard

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(
            "~/Scripts/jquery.unobtrusive*"));

And include the jqueryunobtrusive in _layout.cshtml file.

@Scripts.Render("~/bundles/jqueryunobtrusive")

It will resolve this issue.

Happy Programming :)

Load ASP.NET MVC partial views on link click

Standard

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) {
        $("#Display").html(response);
    });
});
</script>

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 :)

K-MUG KOCHI DEVCON 2015 – March 14

Standard

The Kerala Microsoft Users Group (K-MUG) will be organizing a Developers Conference, K-MUG KOCHI DEVCON 2015 on 14th March 2015 at InfoPark Hall, Athulya Cafeterial Block in Kakkanad. K-MUG is one of the best developer community in India, which got Best User Group award in India as part of Microsoft Community Impact award 2010. DEVCON 2015 will help you to engage and collaborate with Microsoft MVPs and thousands of your IT peers, building connections etc.

DEVCON 2015

DEVCON 2015

You can find more details on DEVCON 2015 Website

I will be speaking on Azure Websites :)