首页 > 解决方案 > 如何将变量传递回控制器中的调用方法

问题描述

我有一个只有一个文本框和搜索按钮的单页 MVC Web 应用程序。最终,我试图检索 youtube API 根据用户搜索的字符串值找到的视频列表。

单击搜索按钮后,它将调用 Home Controller 中的 DisplayVideos 方法。在文本框中键入的字符串的值被传递给此方法,该方法调用另一个名为 RunYouTube 的方法,该方法具有用于查找视频列表的 API 的代码。

我有一个名为 YouTubeVideo 的模型,它包含 2 个字符串属性,一个用于视频 ID,一个用于 VideoTItle。

我能够获取视频的 ID 和标题,但我想知道如何从 RunYouTube 方法中获取此信息(ytv1 或 ytv.ID + ytv.VideoTitle)并返回 HomeController,以便我可以在看法。

[HttpPost]
    public async Task<ActionResult> DisplayVideos(string SearchValue)
    {
        if (ModelState.IsValid)
        {
            YouTubeVideo ytv = new YouTubeVideo();
            SearchYoutube searchObject = new SearchYoutube();
            await searchObject.RunYouTube(SearchValue);

        }

        return View("Index");
    }


public class SearchYoutube
{
    IList<YouTubeVideo> ytvl = new List<YouTubeVideo>();
    YouTubeVideo ytv = new YouTubeVideo();
    public async Task RunYouTube(string searchword)
    {
        var youtubeService = new YouTubeService(new BaseClientService.Initializer()
        {
            ApiKey = "AIzaSyCa5sqGxd-wpYHH3m_TN73WxJNOcm8AHfs",
            ApplicationName = this.GetType().ToString()
        });

        var searchListRequest = youtubeService.Search.List("snippet");
        searchListRequest.Q = searchword; 
        searchListRequest.MaxResults = 50;

        // Call the search.list method to retrieve results matching the specified query term.
        var searchListResponse = await searchListRequest.ExecuteAsync();

        List<string> videos = new List<string>();
        List<string> channels = new List<string>();
        List<string> playlists = new List<string>();

        // Add each result to the appropriate list, and then display the lists of
        // matching videos, channels, and playlists.
        foreach (var searchResult in searchListResponse.Items)
        {
            switch (searchResult.Id.Kind)
            {
                case "youtube#video":
                    videos.Add(String.Format("{0} ({1})", searchResult.Snippet.Title, searchResult.Id.VideoId));

                    ytv.Id = searchResult.Id.VideoId;
                    ytv.VideoTitle = searchResult.Snippet.Title;
                    ytvl.Add(ytv);

                    break;



                    //case "youtube#channel":
                    //    channels.Add(String.Format("{0} ({1})", searchResult.Snippet.Title, searchResult.Id.ChannelId));
                    //    break;

                    //case "youtube#playlist":
                    //    playlists.Add(String.Format("{0} ({1})", searchResult.Snippet.Title, searchResult.Id.PlaylistId));
                    //    break;
            }
        }

        //Console.WriteLine(String.Format("Videos:\n{0}\n", string.Join("\n", videos)));
       // Console.WriteLine(String.Format("Channels:\n{0}\n", string.Join("\n", channels)));
        //Console.WriteLine(String.Format("Playlists:\n{0}\n", string.Join("\n", playlists)));
    }
}

家庭控制器

模型

标签: apimodel-view-controllercontroller

解决方案


我如何从 RunYouTube 方法中获取此信息(ytv1 或 ytv.ID + ytv.VideoTitle)并返回 HomeController,以便我可以在视图上显示信息。

退回它:

public async Task<IList<YouTubeVideo>> RunYouTube(string searchword)
{
  ...
  return ytv1;
}

public async Task<ActionResult> DisplayVideos(string SearchValue)
{
  if (ModelState.IsValid)
  {
    ...
    var ytv1 = await searchObject.RunYouTube(SearchValue);
  }

  return View("Index");
}

推荐阅读