我有一個SelectList,當Text值爲emptyOrNull時,我需要過濾掉嗎?我很努力使這個工作。有任何想法嗎?我已經試過這如何過濾MVC SelectList以刪除不需要的值
示例代碼
// My list - this is valid code
SelectList list = model.MySelectList;
// But when I try this I get the following error message?
SelectList test = list.Where(x => x.Text != "");
我收到以下編譯器錯誤信息
錯誤
Error convert source .. IEnumerable<SelectListItem> to target type SelectList
更新 - 我需要過濾掉值,即我需要類似
foreach (var item in list)
{
if (string.IsNullOrEmpty(item.Text))
{
// remove item from list
}
}
// Then list does not include any items with a Value which is null or empty
UPDATE
進行以下更改後,代碼編譯,但我在所呈現的下拉獲得 'Select.Web.Mvc.SelectListItem'。
new SelectList(list.Where(x => x.Text != ""));
請指教,非常感謝,
謝謝你,這返回的項目,但我怎麼能過濾我們這些項目?謝謝, – user3773890
從哪裏過濾? – Sajeetharan
看到我更新的問題。 – user3773890