从 XML 填充多个下拉列表 (LINQ)



我无法理解的简单 LINQ 查询。 我做了很多谷歌搜索,但无济于事。

我的 XML 文件:

<?xml version="1.0" encoding="utf-8" ?>
<Clients>
  <Client name="TestClient">
    <cat ID="1" value="Computers"/>
    <cat ID="2" value="Ayyy"/>
    <cat ID="3" value="lmao"/>
  </Client>
  <Client name="DoTheNeedful">
    <cat ID="1">ارومیه </cat>
    <cat ID="2">اشنویه </cat>
    <cat ID="3">بوکان </cat>
  </Client>
</Clients>

在我的视图中,模型有一个变量声明为:

public IEnumerable<SelectListItem> Category { get; set; }

我的 LINQ 查询:

        string path = Server.MapPath("~/Controllers/ClientConfig.xml");
        string whichDD = "TestClient";
        var model = new TicketViewModel
        {
            Category =
            from dropDown in XDocument.Load(path).Descendants("Client")
            where dropDown.Attribute("name").Value == whichDD
            from name in dropDown.Descendants("name")                                
            select new SelectListItem
            {
                Value = name.Attribute("ID").Value,
                Text = name.Attribute("value").Value
            }
        };

我的观点:

@model IEGTicketingSite.Models.TicketViewModel
@{
    ViewBag.Title = "Ticket";
}
@Html.DropDownListFor(x => x.CategoryID,
new SelectList(Model.Category, "Value", "Text"))

我正在尝试到达可以引用"TestClient"的位置并获得一个下拉列表

Computers
Ayyy
lmao

Juharr解决了这个问题。

改变:

dropDown.Descendants("name")

自:

dropDown.Descendants("cat")

最新更新