I have simple site
controller.cs
public ActionResult Dodaj()
{
EduSiatkiEntities daneProgramu = new EduSiatkiEntities();
SelectList profileHasel = new SelectList(daneProgramu.SYS_PROFILE_HASEL.Select(e => new{ Value = e.idProfiluHasla, Text = e.nazwaProfilu,}), "Value", "Text");
ViewBag.profileHasel = profileHasel;
ViewBag.CzyZapisanoDane = "nie";
return View();
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Dodaj(Models.UzytkowniDodajkViewModel uzytkownikForm)
{
Debug.WriteLine(uzytkownikForm.idProfilHasla.Where(x => x.Selected=true).FirstOrDefault().Value.ToString());
if (ModelState.IsValid)
{
/**/
}
else
{
ViewBag.CzyZapisanoDane = "nie";
}
return View(uzytkownikForm);
}
view.cshtml
@Html.DropDownListFor(a => a.idProfilHasla, (SelectList)@ViewBag.profileHasel, "Wybierz opcje...", new { @class = "form-control" })
model.cs
[Display(Name = "Profil hasła użytkownika")]
[Required(ErrorMessage = "Wybierz profil hasła użytkownika")]
public IEnumerable<SelectListItem> idProfilHasla { get; set; }
When i debug the program I alway get NULL value from DropDownList (from code below)
Debug.WriteLine(uzytkownikForm.idProfilHasla.Where(x => x.Selected=true).FirstOrDefault().Value.ToString());