简体   繁体   English

ASP.NET MVC。 一个视图中来自同一存储库的多个结果

[英]ASP.NET MVC. Multiple results from the same repository in one View

I'm absolutely beginner in ASP.NET MVC. 我绝对是ASP.NET MVC的初学者。 Here what I'm trying to do for the main (home) page: 这是我要为主页(主页)做的事情:

  • Get last 20 blog post. 获取最后20篇博客文章。
  • Get list of the blog tags. 获取博客标签列表。

As I know it's impossible to use different models in same View, so I'm trying to make calls for different methods of the same repository and then show results. 据我所知,不可能在同一View中使用不同的模型,因此我试图对同一存储库的不同方法进行调用,然后显示结果。 But the problems is - I don't know how to join results. 但是问题是-我不知道如何加入结果。 Should I create new model and pass to View() or should I use partial view with different model? 我应该创建新模型并传递给View()还是应该将局部视图与其他模型一起使用? Here is the Controller code: 这是控制器代码:

public ActionResult Index()
        {
            var top20 = PostRepository.GetLast20();
            var tag = PostRepository.GetTags();
            //How should I return to View() both top20 and tags?
            return View();
        }

PS ActionResult Index contain wrong parameter, so I delete it. PS ActionResult Index包含错误的参数,因此我将其删除。 PSS Probably I should show my repository: PSS可能我应该显示我的存储库:

public class BlogPostRepository : IPostRepo, IDisposable
    {
        private BlogAspNet db;
        public BlogPostRepository(BlogAspNet db)
        {
            this.db = db;
        }
        public IQueryable<blog_post> GetAll()
        {
            return db.blog_post.AsQueryable();
        }
        public IQueryable<tags> GetTags()
        {
            return db.tags.AsQueryable();
        }
        public IQueryable<ShortPostInfo> GetLast20()
        {
            var last = from a in db.blog_post
                       orderby a.Posted descending
                       select new ShortPostInfo
                       {
                           PostID = a.ID,
                           PostSubject = a.Subject,
                           PostAuthor = (from x in db.users where x.ID == a.Author select x.Login).FirstOrDefault(),
                           PostCreated = a.Posted,
                           PostImage = a.PostAvatar !=null ? a.PostAvatar : "other image",
                           PostRating = a.Rating != null ? a.Rating : 0,
                           PostedTags = (from x in db.posted_tags 
                                             join y in db.tags on x.TagID equals y.ID
                                             where x.PostID == a.ID
                                             select y.TagName).ToList()
                       };
            return last.Take(20).AsQueryable();
        }

And here is interface: 这是接口:

  public class ShortPostInfo
    {
        public int PostID { get; set; }
        public string PostSubject { get; set; }
        public DateTime? PostCreated { get; set; }
        public string PostImage { get; set; }
        public string PostAuthor { get; set; }
        public byte? PostRating { get; set; }
        public IList<string> PostedTags { get; set; }
    }
    public interface IPostRepo : IDisposable
    {
        IQueryable<blog_post> GetAll();
        IQueryable<tags> GetTags();
        IQueryable<ShortPostInfo> GetLast20();
        IQueryable<ShortPostInfo> GetPostByTag(int tagid);
        IQueryable<FullPostInfo> GetPostById(int id);
        void Add(blog_post item);
        void Update(blog_post item);
        void Remove(int id);
    }

You can return both results by using an anonymous objects as the model: 您可以通过使用匿名对象作为模型来返回两个结果:

return View(new { top20, tag });

Or if you prefer something stronly typed, you can declare a class to encapsulate your properties: 或者,如果您更喜欢stronly类型的内容,则可以声明一个类来封装您的属性:

public IndexModel {
      public IEnumerable<Post> Top20 { get;set;}
      public IEnumerable<Tag> Tags { get;set;}
}

Then in your Index action: 然后在您的索引操作中:

public ActionResult Index(ShortPostInfo m)
{
    var top20 = PostRepository.GetLast20();
    var tag = PostRepository.GetTags();
    return View(new IndexModel { Top20 = top20, Tags = tag });
}

Finally, you can access your model in your Razor view by using this notation at the top of the file: 最后,您可以使用文件顶部的以下标记在Razor视图中访问模型:

@model IndexModel

And accessing properties by using Model object: 并通过使用Model对象访问属性:

<ul>
     @foreach(var post in Model.Top20) {
         <li>@post.Title</li>
     }
</ul>      

See this article to get further explanation about it (especially "Strongly Typed Models and the @model Keyword" chapter) . 请参阅本文以获取有关它的进一步说明(尤其是“严格类型化的模型和@model关键字”一章)。

You could create a view model where you populate with the data strictly necessary to be displayed in your view. 您可以创建一个视图模型,在其中填充严格必要的数据以在视图中显示。 You would map the fields from each DTO into your view model and render those in your view. 您将把每个DTO中的字段映射到视图模型中,并在视图中呈现这些字段。 You could also as next step use AutoMapper to automatically map your properties. 您还可以在下一步中使用AutoMapper自动映射属性。

https://nerddinnerbook.s3.amazonaws.com/Part6.htm https://nerddinnerbook.s3.amazonaws.com/Part6.htm

http://jasona.wordpress.com/2010/02/05/getting-started-with-automapper/ http://jasona.wordpress.com/2010/02/05/getting-started-with-automapper/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 ASP.NET MVC。 从jQuery调用动作+存储库时出错 - ASP.NET MVC. Error calling action + repository from jQuery ASP.NET MVC。 PagedList通过多个参数过滤 - ASP.NET MVC. PagedList filtering by multiple params ASP.NET MVC。 Autofac和多个连接字符串 - ASP.NET MVC. Autofac and multiple connection strings ASP.NET MVC。 热将数据从视图传输到控制器而不会丢失数据 - Asp.Net MVC. Hot to transmit data from view to controller without dataloss asp.net mvc。 通过Get(表单)发送数据并在标签中返回成功?(如何将数据发送和返回到同一视图) - asp.net mvc. Sending data through Get (form) and returning success in a label?(how to send and return data to the same view) ASP.NET MVC。 如何在没有模型的情况下渲染局部(如创建视图) - ASP.NET MVC. How render a Partial with no model (like create view) 将多个URL路由到ASP.NET MVC中的同一视图 - Routing multiple URLs to the same view in ASP.NET MVC ASP.NET MVC中多个模型的相同视图 - Same view for multiple models in asp.net mvc 在同一个控制器上的多个操作并在asp.net MVC中进行查看 - Multiple actions on the same controller and view in asp.net MVC c# asp.net mvc 一键提交来自多个 forms 的数据在一个视图上 - c# asp.net mvc One Button to submit data from multiple forms on one view
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM