For some reason my SelectedId in my dropdown is always null when I post back the form. The items in the model are not null, just the SelectedID. All of the other values for my model are fine except for the drop down selection. Here is all the code related to my dropdown. Any ideas? Thank you!
P.S - I am using a template for the dropdown and the dropdowns are created automatically from my model when I use @Html.EditorForModel() in my cshtml view.
Template:
@model AFUEMVC.Models.DropDownModels.DropDownViewModel
@Html.DropDownListFor(x => x.SelectedId, new SelectList(Model.Items, "Value", "Text", Model.SelectedId))
Model:
//Dropdowns
[Display(Name = "Fire Type"),]
public DropDownViewModel FireTypeItems { get; set; }
ViewModel:
public class DropDownViewModel
{
public string SelectedId { get; set; }
public IEnumerable<SelectListItem> Items { get; set; }
}
Edit: I am filling my dropdown the following way.
public void GenerateDropDowns(BasicIdentificationModel model)
{
///////////////////////DROP DOWNS
model.FireTypeItems = GetFireTypeItems();
/////////////////////////////////////
}
public DropDownViewModel GetFireTypeItems()
{
DropDownViewModel newdd = new DropDownViewModel();
newdd.Items = new[]
{
new SelectListItem { Value = "1", Text = "IA" },
new SelectListItem { Value = "2", Text = "Extended Attack" },
new SelectListItem { Value = "3", Text = "Large Fire Support" },
};
return newdd;
}
[HttpPost]
public ActionResult BasicIdentificationIndex(BasicIdentificationModel returndata)
{
GenerateDropDowns(returndata);
SaveDB(returndata);
return RedirectToAction("DispatchResponseIndex", "DispatchResponse");
}