从XML填充多个DropDownLists(LINQ)

时间:2015-01-26 20:05:53

标签: c# xml asp.net-mvc linq

简单的LINQ查询,这是我的意思。我做了很多googleing,但没有用。

我的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>

在我的viewmodel中,有一个变量声明为:

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"))

我试图到达我可以参考的地方&#34; TestClient&#34;并使用

获取下拉列表
Computers
Ayyy
lmao

1 个答案:

答案 0 :(得分:1)

juharr解决了这个问题。

更改:

dropDown.Descendants("name")

为:

dropDown.Descendants("cat")
相关问题