[英]ASP.NET MVC return 3 actions in one view?
I got a small problem, I'm trying to display Facebook, Instagram and twitter in one view and order them by date, but i'm not really sure how can i achieve that 我遇到了一个小问题,我试图在一个视图中显示Facebook,Instagram和Twitter,并按日期对其进行排序,但我不确定如何实现该目标
Here is my ActionResult for Twitter, then I have almost identical for Instagram and Facebook 这是我对Twitter的ActionResult,然后对于Instagram和Facebook我几乎完全一样
var twitterContext = new TwitterContext(auth);
ulong sinceId = 1;
const int Count = 20;
var combinedSearchResults = new List<Status>();
var tweets =
await
(from tweet in twitterContext.Status
where tweet.Type == StatusType.User &&
tweet.Count == Count &&
tweet.RetweetedStatus.StatusID == 0 &&
tweet.ExcludeReplies == true &&
tweet.SinceID == sinceId
select new NewsViewModel
{
DateAdded = tweet.CreatedAt,
ScreenName = tweet.User.ScreenNameResponse,
Text = tweet.Text,
StatusId = tweet.StatusID
})
.ToListAsync();
return View(tweets);
}
how can I put all those 3 together and display them in Index view ? 如何将所有这3个放在一起并显示在索引视图中? I'm kinda new to this stuff, would really appreciate the answer and help
我对这个东西有点陌生,非常感谢您的回答和帮助
If I understand you correctly, you want to take all three sources and display them together in a single list. 如果我对您的理解正确,则希望将所有这三个来源都一起显示在一个列表中。
Assuming you can convert all three to your NewsViewModel
type, then just add the results from Facebook and Instagram to the same list, and just pass that one list to your view. 假设您可以将所有三种转换为
NewsViewModel
类型,然后将来自Facebook和Instagram的结果添加到同一列表中,然后将该列表传递到视图中即可。
So you don't use three views - you get data from the three data sources in your controller, then pass the consolidated list to the one view. 因此,您无需使用三个视图-您可以从控制器中的三个数据源获取数据,然后将合并列表传递给一个视图。
Something like this (consolidated, but you get the idea): 像这样的东西(合并,但你明白了):
var posts = await (from tweet ...).ToListAsync(); //what you already have
posts.AddRange(await (from facebook ....).ToListAsync());
posts.AddRange(await (from instagram ....).ToListAsync());
return View(posts);
It can be achived by creating three components direved from Microsoft.AspNetCore.Mvc.ViewComponent
: 可以通过创建三个与
Microsoft.AspNetCore.Mvc.ViewComponent
组件来实现:
public class TwitterViewComponent : ViewComponent
{
public async Task<IViewComponentResult> InvokeAsync(string parameter)
{
...
return View(tweets);
}
}
then create common view Views\\Shared\\Components\\Twitter\\Default.cshtml
or specific for controller view Views\\[controllerName]\\Components\\Twitter\\Default.cshtml
depending on your needs. 然后根据需要创建公用视图
Views\\Shared\\Components\\Twitter\\Default.cshtml
或特定于控制器视图的Views\\[controllerName]\\Components\\Twitter\\Default.cshtml
。
And then you can render your component like this: 然后,您可以像这样渲染您的组件:
<div>
@await Component.InvokeAsync("Twitter", new { parameter = "parameterValue"})
</div>
You can just render child action in your view: 您可以仅在视图中呈现子操作:
<div>
@{Html.RenderAction("Twitter");}
</div>
May be you want this: 可能是您想要这样:
var postList = new PostList();
var twitterPost = await GetTwitterpost(....);
var fbPost = await GetFbPost(....);
var instagramPost = await GetInstagramPost(....);
if(twitterPost !=null)
postList.AddRange(twitterPost);
if(fbPost !=null)
postList.AddRange(fbPost);
if(instagramPost !=null)
postList.AddRange(instagramPost);
postList = postList?.OrderByDescending(p=>p.DateAdded).ToList();
return View(postList);
I don't know if i really understand what you want ! 我不知道我是否真的了解你想要什么! But this way you get all your post in one collection and you can display them by date.
但是通过这种方式,您可以将所有帖子收集到一个集合中,并且可以按日期显示它们。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.