Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7787909
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T20:49:20+00:00 2026-06-01T20:49:20+00:00

I currently have a ViewModel set up for a blog: public class PostViewModel {

  • 0

I currently have a ViewModel set up for a blog:

public class PostViewModel
{
    public string Title { get; set; }
    public DateTime DateCreated { get; set; }
    public string Content { get; set; }
    public int CommentCount { get; set; }
    public ICollection<Topic> Topics { get; set; }
    public ICollection<Comment> Comments { get; set; }
}

Which works perfectly with the controller:

private MyDB db = new MyDB();

public ActionResult Index()
{
    var posts = (from p in db.Set<BlogPost>()
                 select new PostViewModel
                 {
                     Title = p.Title,
                     DateCreated = p.DateCreated,
                     Content = p.Content,
                     Topics = p.Topics,
                     Comments = p.Comments,
                     CommentCount = p.Comments.Count
                 }).ToList();

    return View(posts);
}

Given these two parts, I am able to foreach through the list and generate a blog post with corresponding comments and topics just fine. However, I would like to have a drop down list off to the side that has a list of topics in it. I am guessing I need to alter my ViewModel and HomeController as well, but I am just unsure of how to do that.

@Html.DropDownListFor(???????)

would then go into my Index.cshtml, but I don’t know how I’d deal with that when everything else is coming in as a list?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T20:49:22+00:00Added an answer on June 1, 2026 at 8:49 pm

    You could adapt your view model so that it contains all the necessary information that the view requires. So you mentioned something about a lists of posts and a dropdown of topics. So it is pretty straightforward:

    public class BlogViewModel
    {
        public PostViewModel Post { get; set; }
    
        public string TopicID { get; set; }
        public IEnumerable<SelectListItem> Topics { get; set; }
    }
    

    and then of course you will have to adapt your controller action so that it fetches all the required information from your backend layers and create the proper view model to be passed to the view:

    public ActionResult Index()
    {
        var posts = (from p in db.Set<BlogPost>()
                     select new PostViewModel
                     {
                         Title = p.Title,
                         DateCreated = p.DateCreated,
                         Content = p.Content,
                         Topics = p.Topics,
                         Comments = p.Comments,
                         CommentCount = p.Comments.Count
                     }).ToList();
    
        IEnumerable<Topic> topics = ... go ahead and fetch the topics you want to show in the ddl
    
        var blog = new BlogViewModel
        {
            Posts = posts,
            Topics = topics.Select(t => new SelectListItem
            {
                Value = t.ID,
                Text = t.TopicText
            })
        };
        return View(blog);
    }
    

    and finally the view:

    @model BlogViewModel
    ...
    @Html.DropDownListFor(x => x.TopicID, Model.Topics)
    

    and when you wanted to loop or something over the posts simply use Model.Posts in the view where you previously used Model directly because your view was strongly typed to IEnumerable<PostViewModel>.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose I have ViewModel like public class AnotherViewModel { public string Name { get;
I have a ViewModel that looks like this: public class CreateReviewViewModel { public string
I have the following DTO definition:- [DataContract] public class AddProductDTO { [DataMember] public string
I currently have a need for a custom ListViewItem class - let's call it
I currently have links w/ class=ajax that I want to retrieve the element with
I have a viewmodel class for a create view that consists of 2 properties
I currently have an EF class that is backed by a database view (joining
In an MVC project I have the following classes: public abstract class Browse<T> where
I have a ViewModel class that implements the IDataErrorInfo Interface . In each property's
I have a View and set its DataContext to the corresponding ViewModel. In the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.