aventic
aventic

Reputation: 1514

MVC: Multiple models through controller to view

I'm trying to figure out a way to do this simple blog. I have a frontpage where all blog posts are listed, and when you click on a blog post's title you enter the entire post where you can comment aswell.

My problem is that when I try to pass the models through the controller I only get access to one of my models, either posts or comments.

Here is my models:

public class Post
{
    public int ID { get; set; }
    public string Title { get; set; }
    public string Message { get; set; }
    public string Author { get; set; }
    public DateTime DateTime { get; set; }

    public List<Comment> Comments { get; set; }
}

public class Comment
{
    public int ID { get; set; }
    public int PostID { get; set; }
    public string Message { get; set; }
    public string Author { get; set; }
    public DateTime DateTime { get; set; }
}

Here is my controller:

public ActionResult Details(int id)
{
    **var dbPosts = db.Posts.Find(id);**

    return View(dbPosts);
}

Here is my view:

@model Blog.Models.Post

@{
    ViewBag.Title = "Details";
}

<h2>@Html.ActionLink(Model.Title, "Details", new { id = Model.ID (@Html.ActionLink("Rediger", "Edit", new { id = Model.ID }) - @Html.ActionLink("Slet", "Delete", new { id = Model.ID }))</h2>
<span class="written">skrevet d. @Model.DateTime.ToLongDateString() @Model.DateTime.ToShortTimeString() af @Model.Author</span>
<p>@Model.Message</p>
<hr />

@foreach (var comment in Model.Comments) {
    <span class="written">skrevet d. @comment.DateTime.ToLongDateString() @comment.DateTime.ToShortTimeString() af @comment.Author</span>
    <p>@comment.Message</p>
    <hr />
}

@using (Html.BeginForm()) {
    <label for="Author">Author</label>
    <input type="text" id="Author" name="Author" />

    <label for="Message">Message</label>
    <textarea id="Message" name="Message"></textarea>

    <input type="submit" value="Gem" />
    <input type="reset" value="Reset" />
}

The thing I marked with stars is where I think it goes wrong.. I need to have both my post and comment models passed through this Details view. My question is, how do I do this?

I'm not trying to make anything advanced just trying to learn.

Kind regards and thanks in advance.

Upvotes: 1

Views: 202

Answers (2)

umair.ali
umair.ali

Reputation: 2714

You can also pass it via ViewData, like

public ActionResult Details(int id)
{
    var dbPosts = db.Posts.Find(id);
    // somtthing like that
    var dbComments = db.Comments.Find(dbPost.ID);
    ViewData["KEY_TO_COMMENTS"] = dbComments;
    return View(dbPosts);
}

and then you can access your commects in you view...

@var comments = ViewData["KEY_TO_COMMENTS"] as Blog.Models.Comments;

Hope this helps...:)

Upvotes: 1

Cristi Pufu
Cristi Pufu

Reputation: 9095

You have to map the data from db with your models:

public ActionResult Details(int id)
{ 
       var model = db.Posts.Where(x => x.Id == id).Select(x => new Post(){
            ID = x.Id,
            Title = x.Title,
            Message = x.Message
        }).FirstOrDefault();

        if (model != null)
        {
            model.Comments = db.Comments.Where(x => x.PostId == model.ID).Select(x => new Comment(){
                ID = x.Id,
                PostId = model.ID,
                Message = x.Message
            }).ToList();

            return View(model);
        }
        else
        {
            // post not found
            return View("NotFoundError");
        }   
}

Upvotes: 1

Related Questions