繁体   English   中英

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

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

我绝对是ASP.NET MVC的初学者。 这是我要为主页(主页)做的事情:

  • 获取最后20篇博客文章。
  • 获取博客标签列表。

据我所知,不可能在同一View中使用不同的模型,因此我试图对同一存储库的不同方法进行调用,然后显示结果。 但是问题是-我不知道如何加入结果。 我应该创建新模型并传递给View()还是应该将局部视图与其他模型一起使用? 这是控制器代码:

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包含错误的参数,因此我将其删除。 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();
        }

这是接口:

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

您可以通过使用匿名对象作为模型来返回两个结果:

return View(new { top20, tag });

或者,如果您更喜欢stronly类型的内容,则可以声明一个类来封装您的属性:

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

然后在您的索引操作中:

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

最后,您可以使用文件顶部的以下标记在Razor视图中访问模型:

@model IndexModel

并通过使用Model对象访问属性:

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

请参阅本文以获取有关它的进一步说明(尤其是“严格类型化的模型和@model关键字”一章)。

您可以创建一个视图模型,在其中填充严格必要的数据以在视图中显示。 您将把每个DTO中的字段映射到视图模型中,并在视图中呈现这些字段。 您还可以在下一步中使用AutoMapper自动映射属性。

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

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

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM