开发者

MVC on Edit set selected value when using a IDictionary<string, IEnumerable<SelectListItem>>

Hi I have a line in my view like:

@Html.DropDownListFor(modelItem => item.CheckerId, Model.StaffByArea as IDictionary<string, IEnumerable<SelectListItem>>, "--- Select ---")

The dropdown appears which is great.

However when editing the dropdown is not appearing with a selected value matching CheckerId.

I've seen from other examples how to set the selected value by actually creating your selectlist in the view like:

@Html.DropDownListFor(modelItem => item.CheckerId, new SelectList(Model.StaffList, "Id", "Username", item.CheckerId开发者_运维百科))

However it's really important I use the IDictionary here as I'm trying to create a selectlist with optgroup settings.

So can anyone please help me on how to set the selected value for the dropdown from a IDictionary.

Edit Update:

As requested this is the type of staff by area

StaffByArea = new Dictionary<string, IEnumerable<SelectListItem>>()


Ok, so I found a solution where I populate it in the creation of the Dictionary

@Html.DropDownListFor(modelItem => item.CheckerId, Model.Areas.ToDictionary(s => s.Name, s => s.Users.Where(c => c.AreaId == s.Id).Select(c => new SelectListItem { Value = c.AreaId.ToString(), Text = c.Username, Selected = item.CheckerId == c.AreaId })))
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜