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".