EntityFramework 6 - Load Model and its Dependencies on Home

3

I have a college project in which I decided to do a blog . Something very simple, author, post and comments. only these three. Well

First I wanted the home of the project to load the Blog Posts . I got it. Now I want below each post to give a count in the comments. How can I do this?

Class Codes:

public class Post
    {
        public Post()
        {
        }
        [Required]
        public int PostId { get; set; }
        [Required]
        public int AutorId { get; set; }
        [Required]
        public string Titulo { get; set; }
        [Required]
        public string Conteudo { get; set; }
        public virtual Autor Autor { get; set; }
        public virtual ICollection<Comentario> Comentarios { get; set; }
    }

Controller

public class HomeController : Controller
    {
        private DbContexto db = new DbContexto();

        public ActionResult Login()
        {
            return View(new LoginModel());
        }

        [HttpPost]
        public ActionResult Login(LoginModel model)
        {
            //TODO efetuar o login
            return RedirectToAction("Index");
        }

        public ActionResult Index()
        {
            var query = from e in db.Posts 
                        select e;
            var post = query.ToList();
            return View(post);
        }

        public ActionResult About()
        {
            ViewBag.Message = "Your application description page.";

            return View();
        }

        public ActionResult Contact()
        {
            ViewBag.Message = "Your contact page.";

            return View();
        }
    }

Model

@model IEnumerable<WebBlog.Models.Post>

@{
    ViewBag.Title = "Home Page";
}
<style type="text/css">
    body {
        background: #eee;
    }

    .jumbotron {
        margin-top: 15px;
        background: #fff;
        border: 1px solid #e5e5e5;
            -webkit-border-radius: 5px;
                -moz-border-radius: 5px;
                border-radius: 5px;
            -webkit-box-shadow: 0 1px 2px rgba(0,0,0,.05);
                -moz-box-shadow: 0 1px 2px rgba(0,0,0,.05);
                box-shadow: 0 1px 2px rgba(0,0,0,.05);
    }

    .post-title {
        font-weight: bold;
        font-family: 'Francois One',Tahoma,Verdana,Arial;
        color: #333;
        line-height: 1.4em;
        -ms-word-wrap: break-word;
        word-wrap: break-word;
        text-transform: uppercase;
        padding: 0.3em 0;
        border-bottom: 5px solid #333;
    }

    .postmeta {
        display: block;
        color: #333;
        margin: 0;
        padding: 0.7em 1em;
        background: #eee;
        font-size: 10pt;
        font-weight: bold;
    }

    .entry {
        line-height: 1.6em;
        margin: 0.5em 0;
        -ms-word-wrap: break-word;
        word-wrap: break-word;
    }
    .postinfo{
        margin-bottom: 10px;
    } 
    .postinfo a {
        clear: both;
        line-height: 1.5em;
        display: block;
        color: #333;
        margin: 0;
        font-size: 0.9em;
        display: inline-block;
        float: left;
        color: #fff;
        background: #333;
        padding: 0.4em 1em;
        margin: 0 1px 1px 0;
    }
    .postinfo a:hover {
        background: #e84747;
        text-decoration: none;
    }
</style>
<div class="container">
    @if (this.Model != null && this.Model.Count() > 0)
    {
        foreach (var item in this.Model)
        {
            <div class="jumbotron">
                <div class="post-title">@Html.DisplayFor(modelItem => item.Titulo)</div>
                <div class="postmeta">Postado em 13/05/2014 | por @Html.DisplayFor(modelItem => item.Autor.Nome)</div>
                <div class="entry">@Html.DisplayFor(modelItem => item.Conteudo)</div>
                <div class="postinfo"><a href="#">**@Html.DisplayFor(modelItem => item.Comentarios.Count())** Comentarios</a></div>
            </div>
        }
    }
    else
    {
        <div>Não há posts cadastrados</div>
    }
</div>
    
asked by anonymous 15.05.2014 / 20:32

1 answer

1

Pretty simple. Modify your code to the following:

<div class="container">
    @if (this.Model != null && this.Model.Count() > 0)
    {
        foreach (var item in this.Model)
        {
            <div class="jumbotron">
                <div class="post-title">@Html.DisplayFor(modelItem => item.Titulo)</div>
                <div class="postmeta">Postado em 13/05/2014 | por @Html.DisplayFor(modelItem => item.Autor.Nome)</div>
                <div class="entry">@Html.DisplayFor(modelItem => item.Conteudo)</div>
                <div class="postinfo"><a href="#">@item.Comentarios.Count() Comentarios</a></div>
            </div>
        }
    }
    else
    {
        <div>Não há posts cadastrados</div>
    }
</div>
    
15.05.2014 / 21:06