Using a Razor form (Html.BeginForm()) I am calling an action in my BooksController which making an api call to www.isbndb.com to get book information. After serializing the XML response, I am unsure how to pass that data back to use in the calling view.
Create.cshtml
@using (Html.BeginForm("searchForISBN", "Books"))
{
@Html.TextBoxFor(m => m.Title)
<input class="btn " id="searchForISBN" type="submit" value="Search" />
}
XML response
<?xml version="1.0" encoding="UTF-8"?>
<ISBNdb server_time="2005-07-29T03:02:22">
<BookList total_results="1">
<BookData book_id="paul_laurence_dunbar" isbn="0766013502">
<Title>Paul Laurence Dunbar</Title>
<TitleLong>Paul Laurence Dunbar: portrait of a poet</TitleLong>
<AuthorsText>Catherine Reef</AuthorsText>
<PublisherText publisher_id="enslow_publishers">
Berkeley Heights, NJ: Enslow Publishers, c2000.</PublisherText>
<Summary> A biography of....</Summary>
<Notes>"Works by Paul Laurence Dunbar": p. 113-114.
Includes bibliographical references (p. 124) and index.</Notes>
<UrlsText></UrlsText>
<AwardsText></AwardsText>
</BookData>
</BookList>
</ISBNdb>
BooksController Action
public StringBuilder searchForISBN(Books books)
{
HttpWebRequest request = WebRequest.Create("http://isbndb.com/api/books.xml?access_key=xxxxxx&index1=title&value1="+books.Title) as HttpWebRequest;
string result = null;
using (HttpWebResponse resp = request.GetResponse() as HttpWebResponse)
{
StreamReader reader = new StreamReader(resp.GetResponseStream());
result = reader.ReadToEnd();
}
StringBuilder output = new StringBuilder();
using (XmlReader reader = XmlReader.Create(new StringReader(result)))
{
reader.ReadToFollowing("BookList");
reader.MoveToFirstAttribute();
reader.MoveToNextAttribute();
reader.MoveToNextAttribute();
reader.MoveToNextAttribute();
string shown_results = reader.Value;
int numResults;
bool parsed = Int32.TryParse(shown_results, out numResults);
for (int i = 0; i < numResults; i++)
{
reader.ReadToFollowing("BookData");
reader.MoveToFirstAttribute();
reader.MoveToNextAttribute();
string isbn = reader.Value;
output.AppendLine("The isbn value: " + isbn);
}
}
return ouput;
}
I am trying to return the data to the calling view to display the results below the search-bar form in Create.cshtml. Currently it is returning a new view with the path localhost:xxxxx/Books/searchForISBN.
Any ideas on how to do this?