2017-08-14 60 views
0

有沒有比我在下面的View中使用的更好的方式使用ValueText屬性SelectList?我覺得我正在做一些額外的工作,而不是應該做的。如何使用SelectList中的顯示文本屬性

注意:我所知道的使用下拉菜單與ValueText的其他方式。同時採用SelectList

... 
var customersList = _context.Customers.Select(c => new SelectListItem { Value = c.LastName, Text = c.FullName }); 

MyViewModel.lstCustomers = new SelectList(customersList , "Value", "Text"); 
... 
return View(MyViewModel); 

我發現類似的方法herehere這個問題只涉及到如何實現相同。

+1

'customersList'已經'的IEnumerable '。使用'new SelectList(...)'創建另一個相同的'IEnumerable '只是無意義的額外開銷。你的'lstCustomers'屬性應該是'IEnumerable ',因爲這就是視圖中'DropDownListFor()'方法所需要的。 –

+0

@StephenMuecke這就是我所擔心的 - 做不必要的額外工作。如果我使用'SelectList',我應該如何使用'Value'和'Text'屬性? – nam

+0

你不明白我的意見嗎?它使用了新的'SelectList()',這是毫無意義的。要生成一個''