Search code examples
asp.net-mvcformsenumshtml-helperviewbag

Html Helper Drop Down List switches value to top option on submit / in database


I am filling out a form, however when selecting an option from the drop down list and click submit, no matter what option I select, it always parses the top one through. The displayed value never changes, so it you leave it as the default option 'please select...' and click submit, this stays as 'please select...' but the entry in the database is always the one that appears at the top of the drop down.

Here is the model:

public enum Medium
{
    [Description("Teleconference & Report")]
    Teleconference_Report,
    [Description("Email & Telephone")]
    Email_Telephone
}

[Required]
[Display(Name = "Medium")]
public Medium Medium { get; set; }

Here is the field in the form:

<div class="form-group">
    @Html.LabelFor(model => model.Medium, htmlAttributes: new { @class = "control-label col-md-2" })
    <div class="col-md-5">
        @Html.DropDownList("MediumID", null, "Please select...", htmlAttributes: new { @class = "form-control" })
        @Html.ValidationMessageFor(model => model.Medium, "", new { @class = "text-danger" })
    </div>
</div>

The "MediumID" DropDownList is populated using a viewbag which is set to whatever the following returns:

// Puts all of the mediums of communication into a user friendly dropdownlist.
public List<SelectListItem> GetMediumList()
{
    List<SelectListItem> mediumList = new List<SelectListItem>();

    foreach (Medium state in EnumToList<Medium>())
    {
        mediumList.Add(new SelectListItem
        {
            Text = GetEnumDescription(state),
            Value = state.ToString(),
        });
    }

    return mediumList;
}

Below shows the form section for another enum called 'Frequency', but these are not changed to user friendly strings (and is working fine).

<div class="form-group">
    @Html.LabelFor(model => model.Frequency, htmlAttributes: new { @class = "control-label col-md-2" })
    <div class="col-md-5">
        @Html.EnumDropDownListFor(model => model.Frequency, "Please select...", htmlAttributes: new { @class = "form-control" })
        @Html.ValidationMessageFor(model => model.Frequency, "", new { @class = "text-danger" })
    </div>
</div>

Below here, shows the two methods which turn the enums into user friendly strings:

// Returns a 'user friendly', readable version of the enum.
public static string GetEnumDescription(Enum 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();
}

// Puts all of the same enums into a list.
public static IEnumerable<T> EnumToList<T>()
{
    Type enumType = typeof(T);

    // Can't use generic type constraints on value types,
    // so have to do check like this.
    if (enumType.BaseType != typeof(Enum))
        throw new ArgumentException("T must be of type System.Enum");

    Array enumValArray = Enum.GetValues(enumType);
    List<T> enumValList = new List<T>(enumValArray.Length);

    foreach (int val in enumValArray)
    {
        enumValList.Add((T)Enum.Parse(enumType, val.ToString()));
    }

    return enumValList;
}

Finally, here is the method signature where the fields are binded/bound:

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create([Bind(Include = "Point,ApplicationID,MediumID,Frequency,StartDate,EndDate")] TouchPoint touchPoint)

Within this method, the dropdown is passed to the view using the following:

ViewBag.MediumID = GetMediumList();

Any help is greatly appreciated.


Solution

  • Your model has a property named Medium but your view does not bind to that property. The name of the <select> your generating is MediumID which does not exist in your model, so the default value for Medium when you submit will Teleconference_Report (the first enum value).

    Change the view to

     @Html.DropDownListFor(m => m.Medium, (IEnumerable<SelectListItem>)ViewBag.MediumID, "Please select...", new { @class = "form-control" })
    

    although I would recommend changing the ViewBag property name to say MediumList to make it more obvious that its a collection. And even better, use a view model with a property public IEnumerable<SelectListItem> MediumList { get; set; } so that the viewcan be @Html.DropDownListFor(m => m.Medium, Model.MediumList, .... ).

    You also need to change the [Bind] attribute to include "Medium" (and remove "MediumID") although using a view model means the [Bind] attribute is not required.

    Side note: You do not need the [Required] attribute unless you want to add a specific error message using the ErrorMessage = "..." property (an enum is always required by default unless you make the property nullable).