从 XML 填充多个下拉列表 (LINQ)
Populating Multiple DropDownLists from 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")
我无法理解的简单 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")