views:

177

answers:

3

I realize this is probably a very stupid question so sorry in advanced. I am trying to pass an XML list to a view but I am having trouble once I get to the view.

My controller:

 public ActionResult Search(int isbdn)
    {
        ViewData["ISBN"] = isbdn;
        string pathToXml= "http://isbndb.com/api/books.xml?access_key=DWD3TC34&index1=isbn&value1=";
        pathToXml += isbdn;
        var doc = XDocument.Load(pathToXml);
        IEnumerable<XElement> items = from m in doc.Elements()
                    select m;

What would my view look like? Do I need to incorporate some type of XML data controller?

Thanks and sorry :)

A: 

I'm guessing you've missed some code because you're not assigning to items to the ViewData at any point in this code.

What is happening when you try and access the items in the view, can you add the code from your view to show what you're trying to do?

Glenn Slaven
+2  A: 

I don't know if you intentionally cut off your code half way through the method. But you should be able to do the following to get your elements from your controller action to the view:

ViewData["XmlItems"] = items;

then in your view you call

<% foreach(XElement e in ViewData["XmlItems"] as IEnumerable<XElement>) { %>
    <!-- do work here -->
<% } %>
Nick Berardi
Thanks, I did not know that you could pass a list of items view view data. woops. Stupid like I thought!!!
Marcus Cicero
+2  A: 

first.. you have to return the data to the view modal...

public ActionResult Search(int isbdn)
    {
        ViewData["ISBN"] = isbdn;
        string pathToXml= "http://isbndb.com/api/books.xml?access_key=DWD3TC34&amp;index1=isbn&amp;value1=";
        pathToXml += isbdn;
        var doc = XDocument.Load(pathToXml);
        IEnumerable<XElement> items = from m in doc.Elements()
                    select m;
return view(m);
}

in your code behind you have to inherit

ViewPage < IEnumerable<XElement>>

and than your ViewData.Modal will be a strongly typed IEnumerable<XElement>. and you will be able to work with data as in the controller.

Moran
How do you inherit viewPage from my code behind? Also do you mean return view(items) rather than view(m)?
Marcus Cicero