Wednesday, March 30, 2016

@Html.EnumDropDownListFor in MVC

For MVC v5.1 use Html.EnumDropDownListFor

        @Html.DropDownList("MyType", 
           Html.GetEnumSelectList(typeof(MyType)) , 
           "Select My Type", 
           new { @class = "form-control" })
    

For MVC v5 use EnumHelper

        @Html.DropDownList("MyType", 
           EnumHelper.GetSelectList(typeof(MyType)) , 
           "Select My Type", 
           new { @class = "form-control" })
    

For MVC >5 and lower

I rolled Rune's answer into an extension method:
        namespace MyApp.Common
        {
            public static class MyExtensions{
                public static SelectList ToSelectList(this TEnum enumObj)
                    where TEnum : struct, IComparable, IFormattable, IConvertible
                {
                    var values = from TEnum e in Enum.GetValues(typeof(TEnum))
                        select new { Id = e, Name = e.ToString() };
                    return new SelectList(values, "Id", "Name", enumObj);
                }
            }
        }
    
This allows you to write:
     ViewData["taskStatus"] = task.Status.ToSelectList();
    
by
using MyApp.Common

Alternatively:
Create a custom helper class and use Html.EnumDropDownListFor for all lower version MVC < 5.1 (i.e. 3.x, 4.x, 5.0). Here is the class code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using System.Web.Mvc;
using System.Web.Mvc.Html;
/// 
/// Class ENUM Helper
/// 
public static class EnumHelper
{
    /// 
    /// The single empty item
    /// 
    private static readonly SelectListItem[] SingleEmptyItem = new[] { new SelectListItem { Text = string.Empty, Value = string.Empty } };
    /// 
    /// Gets the ENUM description.
    /// 
    /// The type of the ENUM.
    /// The value.
    /// ENUM Description
    public static string GetEnumDescription(TEnum value)
    {
        FieldInfo fi = value.GetType().GetField(value.ToString());
        DescriptionAttribute[] attributes = (DescriptionAttribute[])fi.GetCustomAttributes(typeof(DescriptionAttribute), false);
        if ((attributes != null) && (attributes.Length > 0))
        {
            return attributes[0].Description;
        }
        else
        {
            return value.ToString();
        }
    }
    /// 
    /// ENUMS the drop down list for.
    /// 
    /// The type of the model.
    /// The type of the ENUM.
    /// The HTML helper.
    /// The expression.
    /// MVC Html String
    public static MvcHtmlString EnumDropDownListFor(this HtmlHelper htmlHelper, Expression> expression)
    {
        return EnumDropDownListFor(htmlHelper, expression, null);
    }
    /// 
    /// ENUMS the drop down list for.
    /// 
    /// The type of the model.
    /// The type of the ENUM.
    /// The HTML helper.
    /// The expression.
    /// The HTML attributes.
    /// MVC Html String
    public static MvcHtmlString EnumDropDownListFor(this HtmlHelper htmlHelper, Expression> expression, object htmlAttributes)
    {
        ModelMetadata metadata = ModelMetadata.FromLambdaExpression(expression, htmlHelper.ViewData);
        Type enumType = GetNonNullableModelType(metadata);
        IEnumerable values = Enum.GetValues(enumType).Cast();
        IEnumerable items = from value in values
                                            select new SelectListItem
                                            {
                                                Text = GetEnumDescription(value),
                                                Value = value.ToString(),
                                                Selected = value.Equals(metadata.Model)
                                            };
        // If the enum is nullable, add an 'empty' item to the collection
        if (metadata.IsNullableValueType)
        {
            items = SingleEmptyItem.Concat(items);
        }
        return htmlHelper.DropDownListFor(expression, items, htmlAttributes);
    }
    /// 
    /// Gets the type of the non null-able model.
    /// 
    /// The model metadata.
    /// real Model Type
    private static Type GetNonNullableModelType(ModelMetadata modelMetadata)
    {
        Type realModelType = modelMetadata.ModelType;
        Type underlyingType = Nullable.GetUnderlyingType(realModelType);
        if (underlyingType != null)
        {
            realModelType = underlyingType;
        }
        return realModelType;
    }
}

Usage:

   @Html.EnumDropDownListFor(model => model.EnumProperty, "Select Enum", new { @class = "form-control", type = "text"})

Monday, February 22, 2016

jQuery cheatsheet

Thanks Abhinay Rathore for providing always up-to-date cheatsheet for jQuery API .

Check the up-to-date cheat sheet here.

jQuery - Coding Standards & Best Practices

These are some of the common standards and best practices for writing better code with jQuery . These standards do not cover JavaScript standards or best practices. Browse the jQuery Cheatsheet for API reference.

Thanks to Abhinay Rathore for such a good article.

The original article can be found Here.

Monday, November 16, 2015

How to use stored procedure with Entity Framework Code First

To learn about how we can use stored procedure with Entity framework code first, jalpesh vadgama has written a very good blog post here.

Thanks jalpesh vadgama.

Working with transaction in Entity Framework 6

In  any Relation database, maintaining integrity of database is very important and Transaction is one way of maintaining database integrity. When you have situation, where you need to insert data into multiple tables and if inserting a data in one of the table fails you should always rollback other inserts transaction becomes very useful. Same scenario can be occurred for update or delete operations. If transactions are not there you will be end up with lots of junk data in tables. Entity framework is one of most popular ORM in Microsoft.NET World. So in this post we are going to learn how we can use transactions with Entity Framework 6.

Read full article here by jalpesh vadgama.

Find a cool free stuff everyday

Giveaway of the Day

Hiren Bharadwa's Posts

DotNetJalps