Showing posts with label MVC4. Show all posts
Showing posts with label MVC4. Show all posts

Tuesday, June 17, 2014

MVC Razor - Convert Enum to List of SelectListItem

To Bind Enum values into Dropdown of MVC Razor, we need to convert them into list of SelectListitem.

Following class helps to convert Enum to List Of SelectListItem

Class:

public static class EnumToSelectedListItem
    {

        public static List<SelectListItem> ToSelectedListItem<T>()
        {
            List<SelectListItem> dropdown = Enum.GetValues(typeof(T)).Cast<T>().
                Select(v => new SelectListItem { Selected = false, Text = v.ToString(), Value = Convert.ToInt32(v).ToString() }).ToList();
            return dropdown;
        }


    }

SelectListItem Class Datastructure  (This class available in MVC)

// Summary:
    //     Represents the selected item in an instance of the System.Web.Mvc.SelectList
    //     class.
    public class SelectListItem
    {
        // Summary:
        //     Initializes a new instance of the System.Web.Mvc.SelectListItem class.
        public SelectListItem();

        // Summary:
        //     Gets or sets a value that indicates whether this System.Web.Mvc.SelectListItem
        //     is selected.
        //
        // Returns:
        //     true if the item is selected; otherwise, false.
        public bool Selected { get; set; }
        //
        // Summary:
        //     Gets or sets the text of the selected item.
        //
        // Returns:
        //     The text.
        public string Text { get; set; }
        //
        // Summary:
        //     Gets or sets the value of the selected item.
        //
        // Returns:
        //     The value.
        public string Value { get; set; }
    }


Consume:

  public List<SelectListItem> PersonEnumList
        {
            get
            {
                return EnumToSelectedListItem.ToSelectedListItem<PersonEnum>();
            }
        }

Tuesday, March 12, 2013

MVC4 - Web Api make work of Put and Delete methods


To use the PUT and DELETE verbs with the Web API in MVC4 
Open ApplicationHost.config from following path
%userprofile%\Documents\IISExpress\config\applicationhost.config 
Add the verbs to the ExtensionlessUrl handler as follows:
Change this line:
<add name="ExtensionlessUrl-Integrated-4.0" path="*." verb="GET,HEAD,POST,DEBUG" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" responseBufferLimit="0"/>
to:
<add name="ExtensionlessUrl-Integrated-4.0" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" responseBufferLimit="0"/>
You can see we have added PUT, DELETE in Verb.

In addition to the above you should ensure WebDAV is not interfering with your requests. This can be done by commenting out the following lines from applicationhost.config.
<add name="WebDAVModule" image="%IIS_BIN%\webdav.dll" />
<add name="WebDAVModule" /> 
<add name="WebDAV" path="*" verb="PROPFIND,PROPPATCH,MKCOL,PUT,COPY,DELETE,MOVE,LOCK,UNLOCK" modules="WebDAVModule" resourceType="Unspecified" requireAccess="None" />
Also be aware that the default Web API convention is that your method name should be the same as the invoked HTTP verb. For example if you're sending an HTTP delete request your method, by default, should be named Delete.