bdd
bdd

Reputation: 3434

Passing a XML parsed list from a controller to a view in ASP.NET MVC

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?

Upvotes: 4

Views: 2239

Answers (3)

Moran Helman
Moran Helman

Reputation: 18562

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&index1=isbn&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.

Upvotes: 3

Nick Berardi
Nick Berardi

Reputation: 54854

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

Upvotes: 3

Glenn Slaven
Glenn Slaven

Reputation: 34203

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?

Upvotes: 0

Related Questions