1

Razor フォーム (Html.BeginForm()) を使用して、ブック情報を取得するために www.isbndb.com への API 呼び出しを行う BooksController でアクションを呼び出しています。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 の新しいビューを返しています。

これを行う方法についてのアイデアはありますか?

4

2 に答える 2

1

XmlReader は、パフォーマンスが重要なコード向けの低レベル API であり、ご想像のとおり、それ自体では、ビューに渡すことができるオブジェクトを作成しません。

isbndb.com によって返されるすべての情報をカプセル化する、厳密に型指定された独自のオブジェクトを作成することを検討してください。ピンチで、XmlDocument クラスを使用し、XPath を使用してそのクラスからプロパティをクエリすることもできます。

    XmlDocument doc = new XmlDocument();
    doc.Load(stream);
    string xpath = "data/element[@id='paul_laurence_dunbar']/AuthorsText";
    XmlNode node = doc.SelectSingleNode(xpath);
    if (node != null) {
          Console.WriteLine(node.InnerText);
    }   

HTH。粘土

于 2012-10-02T19:04:25.223 に答える
0

ビューを直接返すことができます。そのような:

return View("CallingViewName",model);

ただし、ビューに戻したいシリアル化された XML 書籍情報のモデルを構築する必要がある場合があります。

于 2012-10-02T19:07:11.543 に答える