使用 Razor 表单 (Html.BeginForm()) 我在 BooksController 中调用一个操作,该操作对 www.isbndb.com 进行 api 调用以获取图书信息。序列化 XML 响应后,我不确定如何将该数据传回以在调用视图中使用。
创建.cshtml
@using (Html.BeginForm("searchForISBN", "Books"))
{
@Html.TextBoxFor(m => m.Title)
<input class="btn " id="searchForISBN" type="submit" value="Search" />
}
XML 响应
<?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 动作
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;
}
我正在尝试将数据返回到调用视图以在 Create.cshtml 中的搜索栏表单下方显示结果。目前它正在返回一个路径为 localhost:xxxxx/Books/searchForISBN 的新视图。
关于如何做到这一点的任何想法?