0

I have a controller that contains a LINQ query. My query looks something like the following:

var posts = GetBlogPosts();
var results = from post in posts
              select new
              {
                Title = "This is the blog title",
                Url = "www.google.com",
                Author = "Bill Jones",
                AuthorUrl = "www.billjones.com",
                Description = "This is the description of the post"
              };
ViewBag.Posts = results;

I am trying to display the Posts in my view. Currently, I have the following:

@foreach (var post in ViewBag.Posts)
{
  <div class="postBody">
    <h1 class="blogTitle">@post.Title</h1>
    <p class="blogInfo">@post.Description</p>
  </div>
}

When I execute this, I receive an error at runtime. My error says:

'object' does not contain a definition for 'Title'

What am I doing wrong?

0

2 Answers 2

2

That's because this portion of your code is selecting an anonymous object. The view has no idea what type of object that is. Your Viewbag is a dynamic object as well and will not tell the view what specific type of object is located within it.

          select new
          {
            Title = "This is the blog title",
            Url = "www.google.com",
            Author = "Bill Jones",
            AuthorUrl = "www.billjones.com",
            Description = "This is the description of the post"
          };

Most likely you can do the following

 ViewBag.Posts = GetBlogPosts();

In your view you would do this:

@foreach (var post in (IEnumerable<Post>)ViewBag.Posts)
{
  <div class="postBody">
    <h1 class="blogTitle">@post.Title</h1>
    <p class="blogInfo">@post.Description</p>
  </div>
}

Or better yet create a strongly typed view by putting the following at the top of your page

 @model IEnumerable<Post>

In your controller you return View(GetBlogPosts());

And in your view you do

@foreach (var post in Model)
{
  <div class="postBody">
    <h1 class="blogTitle">@post.Title</h1>
    <p class="blogInfo">@post.Description</p>
  </div>
}
Sign up to request clarification or add additional context in comments.

2 Comments

Where should 'Post" definition go? I created a class in the Models folder. However, my .cshtml file still can't find it. I get an error that says: "CS0246: The type or namespace name 'Post' could not be found..."
You can just use the full namespace and type name that is returned from GetBlogPosts() So for example if your GetBlogPosts() returns a List<YourProject.NameSpace.Post> then put @Model List<YourProject.NameSpace.Post>
0

You Can use Partial View instead of using ViewBag becuase ViewBag. You need to create new Action inside your controller then call the action from Parent view using HTML.RenderAction as following 1- Create New Action to contain the Linq code

[ChildActionOnly]
    public ViewResult GetPosts()
    {
    var posts = GetBlogPosts();
    var results = from post in posts
                  select new
                  {
                    Title = "This is the blog title",
                    Url = "www.google.com",
                    Author = "Bill Jones",
                    AuthorUrl = "www.billjones.com",
                    Description = "This is the description of the post"
                  };
    return PartialView("ViewName,posts); // the partial view name ,with posts object
}

2- Now Create a Partial View with strongly-typed view of (Posts) type and give it any name then write your code as you did with little changes.

@model IEnumerable<Post>
@foreach (var post in Model)
{
  <div class="postBody">
    <h1 class="blogTitle">@post.Title</h1>
    <p class="blogInfo">@post.Description</p>
  </div>
}

3- Now in your Parent view add HTML.RenderAction ass following

    <html>
<head>/<head>
    <body>
    ...
    <div>
    @ { Html.RenderAction("GetPosts");  }  <!--ActionName -->
    </div>
    ...
    </body>
</html>

for more information you can check the following links

ChildActionExtensions.RenderAction Method

How to Use the ASP.NET MVC Render Action Helpers

Strongly Typed Views in MVC

RenderPartial vs RenderAction vs Partial vs Action in MVC Razor

When to use ViewBag, ViewData, or TempData

I Hope that help

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.