-1

I have a DropdownlistFor that is not getting the value that is chosen in the DDL. I've done some research and tried implementing some of the suggestions I've seen but mine still isn't working.

I'm using an IEnumerable<SelectListItem> as well as a string property to hold that selected value in my view model.

public IEnumerable<SelectListItem> AvailableSeats { get; set; }
public string Seat { get; set; }

I set the Seat property to an initial value when the ViewModel is created.

Seat = "FO";

This is how I'm populating my SelectListItem. "FO" is always an option but they have "CA" I add that to the SelectList as well.

public static List<SelectListItem> GetAvailableSeats(string currentSeat)
{
    List<SelectListItem> seats = new List<SelectListItem>();
    seats.Add(new SelectListItem { Text = "FO", Value = "FO" });
    if (currentSeat.ToUpper() == "CA")
        seats.Add(new SelectListItem { Text = "CA", Value = "CA", Selected = true });

    return seats;
}

This is my Razor View:

@Html.DropDownListFor(m => m.Seat, Model.AvailableSeats, new { @class = "form-control" })

The HTML that gets produced is:

<select class="form-control" id="Seat" name="Seat">
<option value="FO">FO</option>
<option selected="selected" value="CA">CA</option>
</select>

However, when I select "FO" as my option and then submit my form, the ViewModel still has "CA".

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Caverman
  • 3,371
  • 9
  • 59
  • 115
  • Post your action method. – mxmissile Apr 17 '17 at 17:20
  • There is really nothing to see there. public ActionResult Index(RequestViewModel vm) and when I stop/debug at the beginning of the HttpPost I can see in the VM that the seat shows "CA" even though I chose "FO". – Caverman Apr 17 '17 at 17:55
  • I'm still hoping someone one can help shed some light on this issue. I've been trying a things and still can't get it to work. I haven't tried putting the List into the ViewBag yet but I would prefer to use a ViewModel if possible. – Caverman Apr 18 '17 at 18:16

1 Answers1

0

I finally stumbled across the problem after going through this post.

I'm posting in case it might help someone else. Inside my form I was assigning the the "Seat" property to a hidden field @Html.HiddenFor(m => m.Seat) and I didn't nee to do that. Getting rid of that hidden field fixed the problem.

Caverman
  • 3,371
  • 9
  • 59
  • 115