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 7831579
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T11:47:31+00:00 2026-06-02T11:47:31+00:00

how can i have this model in the view public class PostDetailsViewModel { public

  • 0

how can i have this model in the view

public class PostDetailsViewModel
{
    public Post Posts1 { get; set; }
    public IEnumerable<Post> Posts2 { get; set; }
}

view

@model IEnumerable<forum3.ViewModels.PostDetailsViewModel>
@foreach (var item in Model)
{
   <span>@item.Posts1.Title</span><br />  
   <p>@item.Posts1.Question</p><br /><br />   
}   

controller

  public ViewResult PostDetails(int id)
    {
        PostDetailsViewModel postdetailsviewmodel = new PostDetailsViewModel();
        postdetailsviewmodel.Posts1 = postRepository.Find(id);
        postdetailsviewmodel.Posts2 = postRepository.FindPostByParentIds(id);
        List<PostDetailsViewModel> postDetailsList = new List<PostDetailsViewModel>();
        postDetailsList.Add(postdetailsviewmodel);
        return View(postDetailsList);
    }

how can i have post2 in the view which are the answers

  • 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-02T11:47:33+00:00Added an answer on June 2, 2026 at 11:47 am

    Assuming you want to show a list of questions and its answers and have your View model like this.

    ( I changed Post1 to Question and Posts2 to Answers for better readability)

    public class PostDetailsViewModel
    {
        public Post Question { get; set; }
        public List<Post> Answers { get; set; }
    }
    public class Post
    {
        public int ID { set; get; }
        public string Title { set; get; }
    }
    

    Assuming your controller returns a List of PostDetailsViewModel and PostDetailsViewModel has a Question Property of Type Post and Answers Property of type List

    public ActionResult Posts()
    {
      List<PostDetailsViewModel> objVMLsit = new List<PostDetailsViewModel>();
      //add the object to objVMList here   
    
      return View(objVMLsit);
    }
    

    This View will give you the output

    @model List<MvcApplication1.Models.PostDetailsViewModel>
    @foreach (var item in Model)
    {
      <h2>@item.Question.Title</h2><br />  
      foreach (var answer in item.Answers)
      {
            <p>@answer.Title</p>
      }   
    }
    

    EDIT : After seeing your controller action method, i guess you want to show a specific post ( Question ) and its answers. So Just return one PostViewModel object from the action to your view instead of returning a List of them

    public ViewResult PostDetails(int id)
    {
        PostDetailsViewModel postdetailsviewmodel = new PostDetailsViewModel();
        postdetailsviewmodel.Question= postRepository.Find(id);
        postdetailsviewmodel.Answers= postRepository.FindPostByParentIds(id);
        return View(postdetailsviewmodel);
    }
    

    And change your View like this

    @model MvcApplication1.Models.PostDetailsViewModel
    <h2>@Model.Question.Title</h2><br />  
    @foreach (var answer in Model.Answers)
    {
       <p>@answer.Title</p>
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this model: public class Package { public string CustomerName { get; set;
I have a view model like this: public class EditVM { public Media.Domain.Entities.Movie Movie
i have created the folloiwng view model class public class viewmodelclass { public IEnumerable<Question>
Let's say I have this view model: public class MyModel { [Range(0, 999, ErrorMessage
In the world of MVC I have this view model... public class MyViewModel{ [Required]
I need some help with this one.... I have this simple model: public class
I have something like this: Main view: @model AuthorViewModel @using (Html.BeginForm(Action, Controller, FormMethod.Post, new
Suppose I have this model: public class ViewModel { [Required] public string UserInput {
any help with this would be great. I have a model public class Master
Let's say you have this Model: //model public class Stuff { public string Name

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.