![](/img/trans.png)
[英]How to load items into dropdownlist after selecting item from another dropdownlist in asp.net c#
[英]Remove item from dropdownlist ASP.NET MVC 5 and C#
我的 ASP.NET MVC 5 項目中已經有下拉列表。
我需要刪除一個名為“Admin”的項目參數; 我想在頁面加載時將其從列表中刪除。
這是我的 Razor 標記:
<div class="form-group">
@Html.LabelFor(model => model.RoleName, htmlAttributes: new { @class = "control-label col-md-3" })
<div class="col-md-9">
@Html.DropDownListFor(model => model.RoleName, Model.VMRoles, new { @class = "form
control input-sm", multiple= "multiple" })
@Html.ValidationMessageFor(model => model.RoleName, "", new { @class = "text-danger" })
</div>
</div>
這是 C# controller:
[HttpGet]
[Authorize]
public ActionResult Create()
{
var vm = new CreateUserViewModel
{
VMSisterConcerns = _sisterConcernService.GetAllSisterConcern().Select(c => new SelectListItem { Text = c.Name, Value = c.ConcernID.ToString() }).ToList(),
VMRoles = _roleService.GetAllRole().Select(r => new SelectListItem { Text = r.Name, Value = r.Name }).ToList(),
ConcernId = User.Identity.GetConcernId().ToString()
};
return View(vm);
}
這是 model:
public ICollection<System.Web.Mvc.SelectListItem> VMRoles { get; set; }
這是正確的答案,感謝@itsme86 他提到如何理解 LINQ。
VMRoles = _roleService.GetAllRole().Where(r => r.name != "Admin").Select(r => new SelectListItem { Text = r.Name, Value = r.Name }).ToList(),
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.