我们如何在MVC中绑定groupwise下拉列表 [英] How we bind groupwise dropdownlist in MVC

查看:72
本文介绍了我们如何在MVC中绑定groupwise下拉列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在MVC中组合动态下拉列表项。我的下拉列表只是绑定所有列表项,但我想实现类似optgrouphtml选择选项的下拉列表。任何人都可以帮助我



我尝试过:



这是我的html页面:
< pre> @ Html.DropDownList(cProductionLines,ViewBag.cProductionLines as SelectList,@ Resources.TitleResource.ProductionLines,new {@class =form-control ,@ id =cProductionLines})





这是我的控制器代码:



 private void bindRequestProductionLines()
{
List< COP_ProductList> ProductList = OUnitOfWork.ProductListRepositories.FindBy(x => x.IsActive == true)。ToList();
var ProductionLinesList = ProductList.Select(x => new

{
Id = x.Id,
Type =!CorporateSite.Helpers.CultureHelper.IsLanguageArabic? x.Title_En:x.Title_Ar,
Category =!CorporateSite.Helpers.CultureHelper.IsLanguageArabic?x.COP_ProductionLines.Name_EN:x.COP_ProductionLines.Name_AR,
CategoryId = x.COP_ProductionLines_Id
}) .ToList();

ViewBag.cProductionLines = new SelectList(ProductionLinesList,Id,Type,Category);

}

解决方案

本文应该有所帮助: DropDownList支持HTMLoptgroup - ASP.NET MVC 5.2 | .NET Rumbles [ ^ ]



TL; DR ...从基础开始:

 @ Html.DropDownList(cProductionLines)



现在,如果工作正常,可以添加属性:

 @ Html.DropDownList(cProductionLines,null,new {@class =form-control,@ id =cProductionLines})


I want to bind dynamic dropdownlist items group wise in MVC. My dropdown is simply bind with all list item but I want to achieve dropdownlist similar as "optgroup" html select option. Any one can help me

What I have tried:

This is my html page :
<pre>@Html.DropDownList("cProductionLines", ViewBag.cProductionLines as SelectList,  @Resources.TitleResource.ProductionLines, new { @class = "form-control", @id = "cProductionLines" })



and this is my controller code:

private void bindRequestProductionLines()
        {
            List<COP_ProductList> ProductList = OUnitOfWork.ProductListRepositories.FindBy(x => x.IsActive == true).ToList();
            var ProductionLinesList = ProductList.Select(x => new

            {
                Id = x.Id,
                Type = !CorporateSite.Helpers.CultureHelper.IsLanguageArabic ? x.Title_En : x.Title_Ar,
                Category = !CorporateSite.Helpers.CultureHelper.IsLanguageArabic ? x.COP_ProductionLines.Name_EN : x.COP_ProductionLines.Name_AR,
                CategoryId = x.COP_ProductionLines_Id
            }).ToList();

            ViewBag.cProductionLines = new SelectList(ProductionLinesList, "Id", "Type", "Category");
            
        }

解决方案

This article should help: HTML "optgroup" support in DropDownList – ASP.NET MVC 5.2 | .NET Rumbles[^]

TL;DR ... start with the basics:

@Html.DropDownList("cProductionLines")


Now, if that is working fine, you can add your attributes:

@Html.DropDownList("cProductionLines", null, new { @class = "form-control", @id = "cProductionLines" })


这篇关于我们如何在MVC中绑定groupwise下拉列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆