简体   繁体   中英

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

I'm absolutely beginner in ASP.NET MVC. Here what I'm trying to do for the main (home) page:

  • Get last 20 blog post.
  • 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. 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? 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. PSS Probably I should show my repository:

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:

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:

@model IndexModel

And accessing properties by using Model object:

<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) .

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. You could also as next step use AutoMapper to automatically map your properties.

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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