0

I am trying to create a DropDownList using Html.DropDownListFor. The DropDownList is created but the value I want to select, does not get selected.

Here is my code:

@Html.DropDownListFor(model => model.Nature, new SelectList(TopicNatureHelper.All(), "Key", "Value", 2)

And here is TopicNatureHelper.All():

public static class TopicNatureHelper
{
    public static Dictionary<int, string> All()
    {
        var result = new Dictionary<int, string>();

        foreach (TopicNature topicNature in (TopicNature[]) Enum.GetValues(typeof(TopicNature)))
        {
            result.Add((int)topicNature, TopicNatureHelper.GetLocalizedDescription(topicNature));
        }

        return result;
    }
}

public enum TopicNature
{
    [Description("Owe")]
    Owe = 1,
    [Description("Due")]
    Due = 2,
    [Description("Both")]
    Both = 4
}

As I said, the only problem is that the Selected value is not set. Any clues what is going wrong?

Edit

I debugged the code, and I get these surprising results:

When I am in debug mode, in the watch window, the expression

new SelectList(TopicNatureHelper.All(), "Key", "Value", 2)

creates a System.Web.Mvc.SelectList and the item (with value 2) has Selected=true, so I think it is Html.DropDownListFor that doesn't render the correct output.

To see the result of Html.DropDownListFor, I did this:

@{ var dropDown = Html.DropDownListFor(model => model.Nature, new SelectList(TopicNatureHelper.All(), "Key", "Value", 2));}
@dropDown

Inspecting dropDown gives me this:

// base => Non-public Members => _htmlString
<select class="form-control" data-val="true" data-val-required="this is required" id="Nature" name="Nature">
    <option value="1">Owe</option>
    <option value="2">Due</option>
    <option value="4">Both</option>
</select>

So, I think the problem is with Html.DropDownListFor. I am totally confused.

  • Is the value of property `Nature=2`? –  Sep 21 '14 at 09:05
  • 1
    try [EnumDropDownListFor extension](http://msdn.microsoft.com/en-us/library/dn550802(v=vs.118).aspx) – Grundy Sep 21 '14 at 09:08
  • what type is `model.Nature`? – Grundy Sep 21 '14 at 09:10
  • Or if your not using MVC 5, [How do you create a dropdownlist from an enum in ASP.NET MVC?](http://stackoverflow.com/questions/388483/how-do-you-create-a-dropdownlist-from-an-enum-in-asp-net-mvc) –  Sep 21 '14 at 09:15
  • This may help you: http://stackoverflow.com/questions/19476530/html-dropdownlistfor-selected-value-not-being-set – Nagu_R Sep 22 '14 at 10:35
  • http://stackoverflow.com/questions/19476530/html-dropdownlistfor-selected-value-not-being-set – CelzioBR Mar 23 '17 at 11:35

1 Answers1

0

It was working fine for me in following way. I only removed TopicNatureHelper.GetLocalizedDescription() method as you never pasted the implementation of it, and then I made my own Model - MyModel with a property of TopicNature. I was able to successfully pre-select the value.

public static class TopicNatureHelper
{
    public static Dictionary<int, string> All()
    {
        var result = new Dictionary<int, string>();

        foreach (TopicNature topicNature in (TopicNature[])Enum.GetValues(typeof(TopicNature)))
        {
            result.Add((int)topicNature, topicNature.ToString());
            // I removed TopicNatureHelper.GetLocalizedDescription() method, as I dont have it's implementation
        }

        return result;
    }
}

public enum TopicNature
{
    [Description("Owe")]
    Owe = 1,
    [Description("Due")]
    Due = 2,
    [Description("Both")]
    Both = 4
}

Then I created my model -

public class MyModel
{
    public TopicNature Nature { get; set; }
}

Then this is my simple action method which returns the view with DropDownListFor-

public ActionResult IndexNew()
{
    return View();
}

And this is my view -

@using RamiSamples.Controllers
@model RamiSamples.Controllers.MyModel

@{
    ViewBag.Title = "IndexNew";
}

<h2>IndexNew</h2>

@Html.DropDownListFor(model => model.Nature, new SelectList(TopicNatureHelper.All(), "Key", "Value", 4))

And when I run the page, I get following output -

enter image description here

ramiramilu
  • 17,044
  • 6
  • 49
  • 66
  • I ran your sample code in a new project, and it works fine. But in my own project, it does not work! –  Sep 22 '14 at 07:38
  • Please debug the code and see if all methods are doing exactly fine, there might be some other parts of code which were creating the problem. – ramiramilu Sep 22 '14 at 08:08
  • I did the same thing, and I was able to see the correct markup for @dropdown variable. Can you please tell me what is your model property type of Nature? Also please comment out all other code in that view and run it to check if other parts of the code is not creating this mishap. – ramiramilu Sep 22 '14 at 09:20