{"id":3550,"date":"2014-03-28T10:35:56","date_gmt":"2014-03-28T10:35:56","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/03\/28\/view-not-binding-dropdownlist-selected-value-back-to-model-collection-of-common-programming-errors\/"},"modified":"2014-03-28T10:35:56","modified_gmt":"2014-03-28T10:35:56","slug":"view-not-binding-dropdownlist-selected-value-back-to-model-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2014\/03\/28\/view-not-binding-dropdownlist-selected-value-back-to-model-collection-of-common-programming-errors\/","title":{"rendered":"View not binding dropdownlist selected value back to Model-Collection of common programming errors"},"content":{"rendered":"<p>I am looking for a way to bind an enum in my Model to a dropdownlist. I found this post and used the code from the 2nd answer and it works great in creating the dropdownlist. However, when I submit my form, it always passes the model back with the first value of the enumeration.<\/p>\n<p>Enumeration (this is contained in my Model):<\/p>\n<pre><code>public LayoutType Layout;\npublic enum LayoutType\n{\n    Undefined = 0,\n    OneColumn = 1,\n    TwoColumn = 2,\n    ThreeColumn = 3\n}\n<\/code><\/pre>\n<p>Html Helper Methods:<\/p>\n<pre><code>private static Type GetNonNullableModelType(ModelMetadata modelMetadata)\n    {\n        Type realModelType = modelMetadata.ModelType;\n\n        Type underlyingType = Nullable.GetUnderlyingType(realModelType);\n        if (underlyingType != null)\n        {\n            realModelType = underlyingType;\n        }\n        return realModelType;\n    }\n\n    private static readonly SelectListItem[] SingleEmptyItem = new[] { new SelectListItem { Text = \"\", Value = \"\" } };\n\n    public static string GetEnumDescription(TEnum value)\n    {\n        FieldInfo fi = value.GetType().GetField(value.ToString());\n\n        DescriptionAttribute[] attributes = (DescriptionAttribute[])fi.GetCustomAttributes(typeof(DescriptionAttribute), false);\n\n        if ((attributes != null) &amp;&amp; (attributes.Length &gt; 0))\n            return attributes[0].Description;\n        else\n            return value.ToString();\n    }\n\n    public static MvcHtmlString EnumDropDownListFor(this HtmlHelper htmlHelper, Expression expression)\n    {\n        return EnumDropDownListFor(htmlHelper, expression, null);\n    }\n\n    public static MvcHtmlString EnumDropDownListFor(this HtmlHelper htmlHelper, Expression expression, object htmlAttributes)\n    {\n        ModelMetadata metadata = ModelMetadata.FromLambdaExpression(expression, htmlHelper.ViewData);\n        Type enumType = GetNonNullableModelType(metadata);\n        IEnumerable values = Enum.GetValues(enumType).Cast();\n\n        IEnumerable items = from value in values\n                                            select new SelectListItem\n                                            {\n                                                Text = GetEnumDescription(value),\n                                                Value = value.ToString(),\n                                                Selected = value.Equals(metadata.Model)\n                                            };\n\n        \/\/ If the enum is nullable, add an 'empty' item to the collection\n        if (metadata.IsNullableValueType)\n            items = SingleEmptyItem.Concat(items);\n\n        return htmlHelper.DropDownListFor(expression, items, htmlAttributes);\n    }\n<\/code><\/pre>\n<p>View:<\/p>\n<pre><code>@Html.EnumDropDownListFor(model =&gt; model.Layout)\n<\/code><\/pre>\n<p>On rendering the view, the dropdownlist is fully populated as expected and the right value is selected. But when I submit a POST and pass the value back to my controller, my value for Model.Layout is always &#8220;Undefined&#8221;. Any help would be greatly appreciated! Thanks,<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I am looking for a way to bind an enum in my Model to a dropdownlist. I found this post and used the code from the 2nd answer and it works great in creating the dropdownlist. However, when I submit my form, it always passes the model back with the first value of the enumeration. [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-3550","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/3550","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=3550"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/3550\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=3550"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=3550"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=3550"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}