我已經經歷過這個問題的許多其他版本,但我的嘗試沒有奏效。我有一個通知模型。
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; }
}
使用 NoticeType 表
public class NoticeType
{
[Key]
public int NoticeTypeId { get; set; }
public string Type { get; set; }
}
然后我在我的 Get 中填充選擇串列。
public ActionResult Create()
{
List<NoticeType> _noticeTypes = db.NoticeTypes.ToList();
SelectList noticeTypes = new SelectList(_noticeTypes, "NoticeTypeId", "Type");
ViewBag.NoticeTypes = noticeTypes;
return View();
}
下拉串列中填充了我的表資料。
@Html.DropDownListFor(model => model.Type, ViewBag.NoticeTypes as SelectList, new { @class = "form-control" })
但是,型別值不會傳遞到我的創建帖子。
拜托,有人能解釋一下原因嗎?
謝謝你。
uj5u.com熱心網友回復:
修復課程
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; }
}
并查看
@Html.DropDownListFor(model => model.NoticeTypeId, ViewBag.NoticeTypes as SelectList, new { @class = "form-control" })
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/397077.html
標籤:asp.net-mvc 剃刀
上一篇:列出因子的所有組合
