0

i have an xml file and want to use it to populate a dropdownlist control in asp.net mvc application

xml looks like this

 <?xml version="1.0" encoding="utf-8" ?>
 <Processes>
<Process>
    <id>1</id>
    <ConfigFile>Process1.xml</ConfigFile>
</Process>
<Process>
    <id>2</id>
    <ConfigFile>SecondProcess.xml</ConfigFile>
</Process>
<Process>
    <id>3</id>
    <ConfigFile>Process3.xml</ConfigFile>
</Process>
</Processes>

this is what i have done so far: IT WORKS THANK YOU GUYS

   IEnumerable<SelectListItem> process = from proc in         

                                XDocument.Load("Processes.xml").Descendants("Process")  

                                              select new SelectListItem
                                              {

                                                  Text = (string)proc.Element("ConfigFile")

                                              };
        ViewBag.process = process;

NOW I WANT TO DO SOMETHING ON DROPDOWNLIST ITEMSELECTED EVENT OPEN A NEW WEB PAGE WITH A TEXTBOX

2
  • 1
    What have you tried? Whether the data comes from XML, a database, text, some external service, etc. shouldn't really matter to the UI. By the time the UI sees this it should be loaded into some presentation model, which in this case can be a simple set of key/value pairs from which the UI would build the list items. Commented Feb 4, 2013 at 21:14
  • var process = from proc in XDocument.Load("Processes.xml").Descendants("Process") select proc.Element("ConfigFile").Value.ToList(); Commented Feb 4, 2013 at 21:51

2 Answers 2

1

Try reading the XML and get a List object out of XML and then bind it with the drop down box. That should work!! check XMLReader for reading the XML file.

Sign up to request clarification or add additional context in comments.

Comments

0

Controller:

model.Processes = from p in xdoc.Descendants("Process")
                  let id = (int)p.Element("id")
                  select new SelectListItem 
                  {
                     Selected = (id == model.ProcessId),                    
                     Text = (string)p.Element("ConfigFile"),
                     Value = id.ToString()
                  });

View:

@Html.DropDownList(m => m.ProcessId, Model.Processes)

2 Comments

what is model.processes and could i use var instead ?
model is what you pass to your view. It should have property Processes of type IEnumerable<SelectListItem> and property ProcessId which is selected process

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.