Sonny Childs
Sonny Childs

Reputation: 598

Populating Multiple DropDownLists from XML (LINQ)

Simple LINQ query that is eluding me. I've done lots of googleing, to no avail.

My XML file:

<?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>

In my viewmodel, there's a variable declared as:

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

My LINQ query:

        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
            }
        };

My view:

@model IEGTicketingSite.Models.TicketViewModel

@{
    ViewBag.Title = "Ticket";
}

@Html.DropDownListFor(x => x.CategoryID,
new SelectList(Model.Category, "Value", "Text"))

I'm trying to get to where I can reference "TestClient" and get a dropdownlist with

Computers
Ayyy
lmao

Upvotes: 0

Views: 234

Answers (1)

Sonny Childs
Sonny Childs

Reputation: 598

juharr solved this question.

Changed:

dropDown.Descendants("name")

to:

dropDown.Descendants("cat")

Upvotes: 1

Related Questions