I have gone through many other versions of this question, but my attempt is just not working. I have a notice model.
public class Notice
{
[Key]
public int NoticeId { get; set; }
public string UserId { get; set; }
public string NoticeTitle { get; set; }
public bool Public { get; set; }
public NoticeType Type { get; set; }
}
with a NoticeType Table
public class NoticeType
{
[Key]
public int NoticeTypeId { get; set; }
public string Type { get; set; }
}
I then populate the select list in my Get.
public ActionResult Create()
{
List<NoticeType> _noticeTypes = db.NoticeTypes.ToList();
SelectList noticeTypes = new SelectList(_noticeTypes, "NoticeTypeId", "Type");
ViewBag.NoticeTypes = noticeTypes;
return View();
}
And the dropdown populates with my table data.
@Html.DropDownListFor(model => model.Type, ViewBag.NoticeTypes as SelectList, new { @class = "form-control" })
But, the type value does not get passed to my Create post.
Please, can someone explain why.
Thank you.
CodePudding user response:
fix the class
public class Notice
{
[Key]
public int NoticeId { get; set; }
public string UserId { get; set; }
public string NoticeTitle { get; set; }
public bool Public { get; set; }
public int NoticeTypeId { get; set; }
public virtual NoticeType NoticeType { get; set; }
}
and view
@Html.DropDownListFor(model => model.NoticeTypeId, ViewBag.NoticeTypes as SelectList, new { @class = "form-control" })