Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/31.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# @Html.DropdownList以显示类别>&燃气轮机;子类别>&燃气轮机;作为文本值的子类别_C#_Asp.net_Asp.net Mvc 5_Html Select_Html Helper - Fatal编程技术网

C# @Html.DropdownList以显示类别>&燃气轮机;子类别>&燃气轮机;作为文本值的子类别

C# @Html.DropdownList以显示类别>&燃气轮机;子类别>&燃气轮机;作为文本值的子类别,c#,asp.net,asp.net-mvc-5,html-select,html-helper,C#,Asp.net,Asp.net Mvc 5,Html Select,Html Helper,我有一个简单的Category类来创建一个自引用表 public class Category { public int CategoryId{get; set;} public string Name {get;set; public int? ParentId {get;set} public virtual Category Parent {get;set} public virtual ICollection<Category> Ch

我有一个简单的
Category
类来创建一个自引用表

public class Category
{
    public int CategoryId{get; set;}
    public string Name {get;set;
    public int? ParentId {get;set}
    public virtual Category Parent {get;set}

    public virtual ICollection<Category> Children {get;set;}
}
以及预填充父类别选择的区域

@Html.LabelFor(model => model.ParentId, "Parent Category", new {@class = "control-label"})
@Html.DropdownList("ParentId", null, new {@class ="form-control})
这允许类别具有许多嵌套的子类别和嵌套在其他子类别下的其他子类别,等等。。。等等

“创建”视图允许您创建新类别,并使用
@Html.DropdownList
分配父类别,该列表提取所有类别的文本值,但仅列出实际类别或子类别名称

是否有方法更改
@Html.DropdownList
中的显示值,以显示层次结构树而不是单个父级值

因此,它不是显示“AAA电池”(新类别父类别的值)的
@Html.Dropdownlist
,而是显示父类别的完整分层值:

Electronic Supplies >> Batteries >> AAA Batteries

这当然是一个名为“电子用品”的类别,其子类别为“电池”,子类别为“AAA电池”。您需要在模型中为您生成此名称的方法。比如:

public class Category
{
    public int CategoryId {get; set;}
    public int ParentId {get; set;}
    public string Name {get; set;}

    public string GetFullCategoryName()
    {
        string result = this.Name;

        // note: I removed the argument because it's class member.
        Category parent = this.GetParent(); // null for top level Categories

        while (parent != null)
        {
            result = parent.Name + " >> " + result;
            parent = GetParent(parent.ParentId);
        }

        return result;
    }

    public Category GetParent()
    {
        // note: I just made this up, you would use whatever EF method you have...
        return EF.GetEntity<Category>(this.ParentId);
    }
}
编辑2:
我更改了上面的代码以显示整个类,也许这会更有意义?

谢谢@JimG。对于如何构造GetParent方法以提供GetFullCategoryName()方法,我有点不知所措。。。你能帮忙吗?我想不出一个合适的方法来这么做…抱歉,这比我以前尝试过的任何方法都要复杂一些…没问题,GetParent将是一个接受ParentId并从EF返回相应类别的方法。然而,如果不了解更多关于实体和关系等的信息,我就不能肯定这对您是有效的。这个方法可以在类别中,也可以是某个单独的静态方法,或者是您喜欢的任何方法。我使用的方法是,GetParent和GetFullCategoryName都将在您的Category类中定义,但总有很多方法可以做到这一点。(我应该把
this.GetParent(this.ParentId)
放进去,也许会更清楚。)谢谢大家。当我深入到一个需要我扩展所学知识的项目中时,我感谢你的帮助和帮助,没有什么现成的方法可以做到这一点。我发表了一篇关于代码项目的文章,源代码显示了一种可能性。其他选项包括使用与类别文本垂直的父类别文本构建选项,或者基于父元素的选择(使用jquery和ajax)为子元素动态构建额外的dropdownlists@StephenMuecke谢谢!代码项目示例肯定会有所帮助。当我深入MVC时,我感谢大家的耐心和对我的帮助!
public class Category
{
    public int CategoryId {get; set;}
    public int ParentId {get; set;}
    public string Name {get; set;}

    public string GetFullCategoryName()
    {
        string result = this.Name;

        // note: I removed the argument because it's class member.
        Category parent = this.GetParent(); // null for top level Categories

        while (parent != null)
        {
            result = parent.Name + " >> " + result;
            parent = GetParent(parent.ParentId);
        }

        return result;
    }

    public Category GetParent()
    {
        // note: I just made this up, you would use whatever EF method you have...
        return EF.GetEntity<Category>(this.ParentId);
    }
}
@Html.DropDownListFor(x => x.CategoryId, Model.GetAllCategories().Select(c => new SelectListItem() { Text = c.GetFullCategoryName(), Value = c.CategoryId }))