DropDownListFor具有所有项

时间:2017-08-01 09:59:45

标签: c# razor asp.net-mvc-5

我正在使用剃须刀助手DropDownListFor,以便从Active Directory获得组织单位的下拉列表(在数据库中复制,它并不重要)。我的想法是你有一个包含所有父元素的第一个列表,当点击时,我搜索我的数据库以找到子元素,依此类推,直到你到达一个叶子(可以这么说,最后一个元素)。我的问题是,当我在浏览器中检查元素时,我发现value标记中的所有<option>都等于我点击时传递的第一个ID。

所以这是我的ViewModel:

public class SiteDirectionModel
{
    public int id { get; set; }
    public List<SelectListItem> Names { get; set; }
}

然后我的第一个控制器方法(对于父元素)如下:

public ActionResult CreateADUser()
{
    List<SelectListItem> items = new List<SelectListItem>();
    SiteDirectionModel sdM = new SiteDirectionModel();

    //The below method gets all parent nodes from database
    //_nonGenService is an instance of a service that can access my Repository
    direction = _nonGenService.GetAllSiteDirection();
    if (direction != null && direction.Count!=0)
    {
        items.Add(new SelectListItem
        {
            Text = dir.Name, 
            Value = dir.IDDirection.ToString()
        });
     }
     sdM.Names = items;
     return View(sdM);
    }
    else
        return View("AccessError"); //A page in case of error 
}

然后是第二个控制子元素的控制器方法:

public ActionResult GetSiteRF(SiteDirectionModel model)
{
    SiteDirectionModel sdM = new SiteDirectionModel();
    List<SelectListItem> items = new List<SelectListItem>();
    //This method gets children elements from a specified id
    //The table that looks has IDDirection as FK from first table
    radioFrance = _nonGenService.GetSiteRFFromDirection(model.id);
    direction = _nonGenService.GetAllSiteDirection();
    int id;
    string nameConcat = string.Empty;
    //For loop to concatenate names
    foreach(var dir in direction)
    {
        if (dir.IDDirection == model.id)
        {
            nameConcat = dir.Name + "-" + nameConcat;
            break;
        }
    }
    if (radioFrance.Count==1)
    {//This condition would be to see if the name is equal to the current node so 
     //that we can exit the condition and access a form to create the user
        Site_RadioFrance single = radioFrance.SingleOrDefault();
        if (single.Name.Equals(nameConcat))
        {
            return View("CreateADForm", model);
        }
    }
    else
    {
        foreach (var radio in radioFrance)
        {
            items.Add(new SelectListItem
            {
                Text = nameConcat+radio.Name,
                Value = radio.IDDirection.ToString()
            });  
        }
        sdM.Names = items;
        return View("CreateADUser",sdM);
    }
    return View("AccessError"); //Error treatement
}

我的观点是这样的:

@model MyProject.Models.SiteDirectionModel
<h2>Create an account in Active Directory</h2>
@using (Html.BeginForm("GetSiteRF", "Create", FormMethod.Post))
{
    @Html.DropDownListFor(x => x.id, Model.Names);
    <input type="submit" value="Selectionner" class="btn btn-primary"/>
}

现在说您点击了IDDirection=10项目,然后我的所有子元素都

<option value=10>parent1-child1</option>
<option value=10>parent1-child2</option>
<option value=10>parent1-child3</option>
<option value=10>parent1-child4</option>

我不知道如何解决这个问题,任何想法?因为那时我的模型ID有这个值,我不知何故认为它会为每个不同的选项应用一个值,我不明白为什么它没有?

谢谢!

0 个答案:

没有答案