2017-08-17 48 views
0

我喜歡婁代碼查詢我是新來的ASP.Net MVC:如何在ASP.Net MVC創建下拉列表

var MyCompanies = _context.MyCompanyInfoContext.Where(u => u.UserId == user.Id).OrderBy(o => o.CompanyName); 

如何產生鑑於

我的視圖代碼下拉列表:

@Html.DropDownListFor(model=>model.CompanyId, new SelectListItem(ViewBag.MyCompanies)) 

,但我得到的錯誤

'SelectListItem' 確實ñ OT包含一個構造函數1個參數

+0

'新的SelectList(ViewBag.MyCompanies, 「CompanyId」, 「公司名稱」))''假設MyCompanyInfoContext'包含屬性'CompanyId'和'CompanyName' –

回答

2

請重碼波紋管這應該工作:

@Html.DropDownListFor(model => model.CompanyId, new SelectList(ViewBag.MyCompanies, "Id", "CompanyName"), "- Please Select -", new { @required = "required", @class = "form-control " }) 
+0

謝謝你爲我工作,非常感謝。 – CodeOnly