4

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?

3 Answers 3

3

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.

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

1 Comment

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

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

1 Comment

Thanks, I did not know that you could pass a list of items view view data. woops. Stupid like I thought!!!
0

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?

Comments

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.