2014-04-16 136 views
0

我有一个表类别ID和父类别ID。类别和子类别单下拉列表中为 - MVC剃刀

Category Id Category Name Parent Category Id 
    1   Desktop    0 
    2   PC     1 
    3   Mac     1 

如何在下拉列表返回它在下面的格式:

enter image description here

+0

你想要每个项目的价值是什么? – bsayegh

+0

@bsayegh,我不明白你的意思是“你想要每个项目的价值是什么?”。我想返回一个查询来以这种格式填充下拉列表,例如类别ID - >父类别ID。 – uikrosoft

回答

0

我能想到的最好的事情是创建一些类保持名称和值一下拉列表并使用它在你的Action中创建一个SelectList。

public class SelectListValue{ 
    public string Text {get;set;} 
    public string Value {get;set;} 
} 

public ActionResult SomeActionMethod() 
{ 
    var categories = GetCategories() //Get your list of categories 
    var selectListItems = new List<SelectListValue>(); 

    foreach(var item in categories) 
    { 
    var text = GetCategoryDisplay() //You will need to figure out how to generate the text, like Components > Web Cameras. This doesn't need to be a method. It could be a property on the Category object. 

    selectListItems.Add(new SelectListValue {Text = text, Value = item.CategoryId.ToString()}); 
    } 

    ViewData["CategoryList"] = new SelectList(selectListItems, "Value", "Text"); 
} 

您将需要有一种方法,可能在您的模型中,以生成名称字符串。我猜你已经这么做了。然后在你看来,你会做类似

@Html.DropDownListFor(x => x.CategoryId, ViewData["CategoryList"] as SelectList) 

这应该工作,或至少是这样的。

1

嗨,哥们我找了这么多,我找不到它,但最后我自己编码。

我会试着告诉你我是如何解决这个问题的,并且像我的截图一样工作得很好。这里是我的secreenshot

这里是我的类模型,它几乎与UR模式相同

public class Category 
 
      { 
 
       public int ID { get; set; } 
 
       public string CategoryName { get; set; } 
 

 
       public int? ParentID { get; set; } 
 

 
       [ForeignKey("ParentID")] 
 
       public virtual ICollection<Category> SubCategories { get; set; } 
 
      }

然后我创建的视图类型模型这样

public class ViewModelCategory 
 
    { 
 
     public Category Category { get; set; } 
 
     public List<SelectListItem> Categories { get; set; } 
 
     public List<SelectListItem> ChildCategories { get; set; } 
 

 
     public List<SelectListItem> AllCategories 
 
     { 
 
      get 
 
      { 
 
       List<SelectListItem> sli = new List<SelectListItem>(); 
 
       foreach (SelectListItem item in Categories) 
 
       { 
 
        sli.Add(item); 
 
       } 
 
       foreach (SelectListItem item in ChildCategories) 
 
       { 
 
        int i = item.Text.IndexOf(">"); 
 
        string text = item.Text.Substring(0, i-1); 
 
        foreach (SelectListItem listItem in ChildCategories) 
 
        { 
 
         int k = listItem.Text.LastIndexOf(">"); 
 
         string text2 = listItem.Text.Substring(k+1); 
 
         if (text2.Contains(text)) 
 
         { 
 
          item.Text = listItem.Text + " > " + item.Text.Substring(i+2); 
 
         } 
 
        } 
 
        sli.Add(item); 
 
       } 
 
       return sli; 
 
      } 
 
     } 
 
    }

我的代码可能看起来很复杂,但实际上并不是。

当我看着模型时,会看到3个selectlistitem属性,在我的ViewModelCategory模型中有一些重要的东西。我想先谈谈这些。

我创建了其中一个名为“类别”的主类别没有任何父母id值。 其中第二个叫做“ChildCategories”仅包含具有父母ID值的子类别

当你看到他的控制器代码时,你会看到。

和最后一个叫做“AllCategories”。这是一个重要的一步,我将这个属性中的所有类别合并为控制器视图中的类别下拉列表。但它不仅仅是为了这个,我也在这个属性中做了一些重要的事情,当我看到你将看到的循环时,我将它们合并了。我使用的环路放置类别,他们的父母

然后用我的ViewModelCategory模型控制器这样

ViewModelCategory vmc = new ViewModelCategory 
 
      { 
 
       Category = category, 
 
       Categories = _businessCategories.GetItems().Where(x => x.ParentID == null).Select(x => new SelectListItem 
 
       { 
 
        Text = x.CategoryName, 
 
        Value = x.ID.ToString() 
 
       }).ToList(), 
 
       
 
       ChildCategories = _businessCategories.GetItems().Where(x=>x.ParentID != null).Select(x => new SelectListItem 
 
       { 
 
        Text = _businessCategories.GetItems().Where(a => a.ID == x.ParentID).FirstOrDefault().CategoryName + " > " + x.CategoryName, 
 
        Value = x.ID.ToString() 
 
       }).ToList(), 
 
      };

最后我欢迎我DATAS从视图这样

<div class="form-group"> 
 
        <label class="col-sm-4 control-label">Sub Category</label> 
 
        <div class="col-sm-8"> 
 
         @Html.DropDownListFor(model => model.Category.ParentID, Model.AllCategories.OrderBy(x=>x.Text), "Select Category", new {@class = "form-control"}) 
 
         @Html.ValidationMessageFor(model => model.Category.ParentID) 
 
        </div> 
 
       </div>

希望你会喜欢这有美好的一天

相关问题