如何将变量传递回我的contoller中的调用方法

时间:2019-05-16 10:25:18

标签: api model-view-controller controller

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

单击搜索按钮后,它将在家庭控制器中调用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)));
    }
}

home controller

model

1 个答案:

答案 0 :(得分:0)

  

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

return

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");
}