Skip to main content
 首页 » 编程设计

c#之如何在选择另一个@Html.DropDownList 后填充@Html.DropDownList

2024年02月13日8kuangbin

<分区>

加载了一些类别的 DropDownList,我需要根据在第一个下拉列表中选择的选项填充另一个 @Html.DropDownList。

这是我用来填充第一个下拉列表的代码:

在 Controller 上:

        TecnicService ListCategory = new TecnicService(); 
        IList<Category> resultCat = ListCategory.GetCategory(); 
 
        List<SelectListItem> CatDropDown = new List<SelectListItem>(); 
 
        foreach (Category in resultadoCat) 
        { 
            CatDropDown.Add(new SelectListItem 
            { 
                Value = a.Id.ToString(), 
                Text = a.Name.ToString() 
            }); 
        } 

在 View 上:

  @model APP.Models.DataViewModel   
    @using (Html.BeginForm("NewPol", "Tecnic", null, FormMethod.Post, new { id = "pol-data-form", @class = "form-horizontal" })) 
    { 
    <div class="control-group"> 
             <label for="category" class="control-label">Category</label>               
             <div class="controls"> 
                  @Html.DropDownList("BasicData", Model.Category, new { @class= "required", name="category"}) 
             </div>                
        </div>     
        <div class="control-group"> 
             <label for="ram" class="control-label">Ram</label> 
              <div class="controls"> 
                  @Html.DropDownList() WHAT DO I DO HERE??????? 
              </div> 
        </div> 
. 
.} 

我通过返回列表的服务获取数据,现在我需要根据第一个下拉列表的选择填充第二个下拉列表。