Passing an 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 problems when I get 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 opinion look like? Do I need to include some type of XML data controller?

+4
source share
3 answers

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

 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 need to inherit

 ViewPage < IEnumerable<XElement>> 

and your ViewData.Modal will be strongly typed by IEnumerable<XElement> . and you can work with data, as in a controller.

+3
source

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

 ViewData["XmlItems"] = items; 

then, in your opinion, you cause

 <% foreach(XElement e in ViewData["XmlItems"] as IEnumerable<XElement>) { %> <!-- do work here --> <% } %> 
+3
source

I assume that you missed some code because you are not assigning ViewData elements anywhere in this code.

What happens when you try to access elements in a view, can you add code from your view to show what you are trying to do?

0
source

All Articles