摘要:MVC 動態新增select list(dropdown list)到view中 (for 新增,修改頁面)
01.先在controller頂端創造一個class
public class MemberController : Controller
{
//會員類別
public class accessID
{
public int aID { get; set; }
public string Name { get; set; }
}
02.在Action Result中動態加入dropdownlist的值
public ActionResult Add()
{
MANAGER_DATA_RegisterModel mdr = new MANAGER_DATA_RegisterModel();
//動態創造select,加入狀態
List ls = new List();
ls.Add(new accessID { aID = 1, Name = "test" });
ls.Add(new accessID { aID = 2, Name = "test2" });
IEnumerable accessIDs = ls;
SelectList sl = new SelectList(accessIDs, "aID", "Name");
ViewData["sl"] = sl;
return View(mdr);
}
03.在view中接收controller傳來的VIEWDATA
@Html.DropDownListFor(m => m.MA_ID, (SelectList)ViewData["sl"])