我有一个包含类别 ID 和父类别 ID 的表。
Category Id Category Name Parent Category Id
1 Desktop 0
2 PC 1
3 Mac 1
如何以以下格式在下拉列表中返回它:
我有一个包含类别 ID 和父类别 ID 的表。
Category Id Category Name Parent Category Id
1 Desktop 0
2 PC 1
3 Mac 1
如何以以下格式在下拉列表中返回它:
嗨,哥们,我找了这么多,但我找不到,但最后我自己编码了。
我会试着告诉你我是如何解决这个问题的,并且效果很好,就像你的截图一样。这是我的截图
这是我的类别模型,与您的模型几乎相同
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;
}
}
}
我的代码看起来有点复杂,但实际上并非如此。
当您查看模型时,我的 ViewModelCategory 模型中有一些重要的东西,您将看到 3 个 selectlistitem 属性。我想先谈谈这些。
我为没有任何父 id 值的主要类别创建了其中一个,称为“类别”。其中第二个称为“ChildCategories”仅包含具有父 id 值的子类别
当您查看他的控制器代码时,您已经看到了。
最后一个称为“AllCategories”。这是一个重要的原因,我合并了这个属性中的所有类别,以便在控制器的视图中将类别发送到 dropdownlisfor。但不仅仅是为了这个,我还在这个属性中做了一些重要的事情,当你查看循环时,我将它们合并,你会看到它。我使用循环将类别放置给他们的父母
然后像这样在控制器中使用我的 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(),
};
最后我欢迎我的数据来自这样的视图
<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>
希望你会喜欢这有一个美好的一天
我能想到的最好的事情是创建一些类来保存下拉列表的名称和值,并使用它在您的操作中创建一个 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)
这应该有效,或者至少是类似的。