1

I'm sorry, for I'm sure there's a way to do this with a viewModel, however I'm very inexperienced with this and don't even know if I'm doing it correctly.

What I'm trying to do is pass multiple blogs and the profile info of the user who posted each blog to a view.

I'm getting the following error.

The model item passed into the dictionary is of type 'ACapture.Models.ViewModels.BlogViewModel', but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable`1[ACapture.Models.ViewModels.BlogViewModel]'.

I'm trying to pass the following query results to the view.

        var results = (from r in db.Blog.AsEnumerable()
                       join a in db.Profile on r.AccountID equals a.AccountID
                       select new { r, a });

        return View(new BlogViewModel(results.ToList()));
    }

This is my viewModel

public class BlogViewModel
{
    private object p;

    public BlogViewModel(object p)
    {
        this.p = p;
    }
}

And my view

@model IEnumerable<ACapture.Models.ViewModels.BlogViewModel>
@{
    ViewBag.Title = "Home Page";
}

<div class="Forum">
    <p>The Forum</p>

            @foreach (var item in Model)
            {
                <div class="ForumChild">


                    <img src="@item.image.img_path" alt="Not Found" />
                    <br />
                        <table>
                            @foreach (var comment in item.comment)
                            {

                                <tr><td></td><td>@comment.Commentation</td></tr>
                            }
                        </table>
                </div>                
            }
</div>

Thanks in advance.

2 Answers 2

2

I guess you need to change your view model a little to:

public class BlogViewModel
{
    public Blog Blog { get; set; }
    public Profile Profile{ get; set; }
}

and then return it as follow:

    var results = (from r in db.Blog.AsEnumerable()
                   join a in db.Profile on r.AccountID equals a.AccountID
                   select new new BlogViewModel { Blog = r, Profile = a });

    return View(results.ToList());

Then in your foreach loop inside of view, you will get an objects that will contain both - profile and blog info, so you can use it like f.e. @item.Profile.Username

Sign up to request clarification or add additional context in comments.

Comments

0

I'm not entirely sure what you're trying to accomplish with the ViewModel in this case, but it seems like you are expecting for the page to represent a single blog with a collection of comments. In this case you should replace

IEnumerable<ACapture.Models.ViewModels.BlogViewModel>

With

ACapture.Models.ViewModels.BlogViewModel

Then Model represents a single BlogViewModel, that you can iterate over the comments by using Model.comments and access the image using Model.image.img_path.

If this not the case, and you intend to have multiple BlogViewModels per page, then you will have to actually construct a collection of BlogViewModels and pass that to the view instead.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.