api - 如何将变量传递回控制器中的调用方法
问题描述
我有一个只有一个文本框和搜索按钮的单页 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)));
}
}
解决方案
我如何从 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");
}
推荐阅读
- c# - 使用 C# 或 Linq 对 MongoDB 和 SQL 使用相同的语法
- mongodb - 在 MongoDb 中按 15 分钟的时间间隔对重叠系列进行分组
- android - 如何使用本地通知获取有关 Firebase 聊天应用程序的通知?
- xamarin.forms - Packacge Sqlite.net.platform.xamarinAndroidN 3.1.1 不兼容 monoandroid9(MonoAndroid, version=v9.0)
- interceptor - NestJS:如何将 ClassSerializerInterceptor 设置为全局拦截器
- python - SMTP | Python - 错误:身份验证失败
- python - 如何使用 Python 或正则表达式在引号中获取对话行?
- angular - 在 3 个选择选项下拉列表之间使用 ngModel 绑定数据时出现问题。(角度)
- python - 岛屿数量的递归解决方案
- mysql - 在从同一组中的前一行减去该行之前将空值替换为零