簡體   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