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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T23:59:15+00:00 2026-06-15T23:59:15+00:00

I’m trying to do something i that feels like a small task, but i

  • 0

I’m trying to do something i that feels like a small task, but i cannot figure out a simple way to do it. All my approaches for doing this gets really complex for a simple task.

I have these models:

public class Blog
{
    public int Id { get; set; }
    public String Name { get; set; }
    public virtual ICollection<Post> Posts { get; set; }
}

public class Post
{
    public int Id { get; set; }
    public String Name { get; set; }
    public int BlogId { get; set; }
    public virtual ICollection<Comment> Comments { get; set; }
}

public class Comment
{
    public int Id { get; set; }
    public String CommentText { get; set; }
    public int PostId { get; set; }
    public int UserProfileUserId { get; set; }
}

public class UserProfile
{
    public int UserId { get; set; }
    public string UserName { get; set; }
    public String FirstName { get; set; }
    public String LastName { get; set; }
}

In the Added Comments partial view, i want to show the full user name of the user that made a comment. If i just use my base classes in my views and partial views, i get everything i need except full user name on added comments. So far, i’ve thought of the following ways:

ViewModels – This will result in creating a ViewModel for each of my Classes and then populate / map them manually in my controller.

Code in Views – I have the UserProfileUserId so i can just ask the repository from the view but this Kills the MVC in MVC so i don’t want to do it.

Actually Adding UserProfileFirstName and UserProfileLastName to the Comment Class as foreign keys – This feels like filling the database with view specific data. It doesn’t belong in a relational database.

Using regular SQL and Query the database – Just because i know SQL, this -could- be a way to do it. but then again i’m killing the MVC in MVC.

How should i do this? Where is my silly overlooked option? I’ve searched a lot but could not find an answer, but this could be related to me not knowing all the technical terms yet. Sorry if this is answered 1000 times before.

  • 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-15T23:59:15+00:00Added an answer on June 15, 2026 at 11:59 pm

    Ideally i would change my domain model to include a Author property of type UserProfile and load that data as well using a JOIN (Comment table and User table)

    public class Comment
    {
        public int Id { get; set; }
        public String CommentText { get; set; }
        public int PostId { get; set; }
        public UserProfile Author { get; set; }
    }
    

    EDIT : As per the questions in the comment

    This is how i will do this.

    My Repositary method will have these methods

    List<Comment> GetCommentsForPost(int postId);
    BlogPost GetPost(int postId);
    

    I would have ViewModel for representing a single blog post like this

    public class PostViewModel
    {
       public int PostID { set;get;}
       public string PostText { set;get;}
       public string AuthorDisplayName { set;get;}
       public List<CommentViewModel> Comments { set;get;}
    
       public PostViewModel()
       {
          Comments=new List<CommentViewModel>();
       }
    }
    public class CommentViewModel
    {
      public int CommentID {set;get;}
      public string Text { set;get;}
      public string AuthorDisplayName { set;get;}  
    }
    

    Now in your GET Action, Get the data from your Repositary and Map that to ViewModel and send it to view

    public ActionResult ViewPost(int id)
    {
    
      var post=repositary.GetPost(id);
      if(post!=null)
      {
        PostViewModel vm=new PostViewModel { PostID=id };
        vm.PostText=post.Name;
        var comments=repo.GetCommentsForPost(id);
        foreach(var item in comments)
        {
          vm.Comments.Add(new CommentViewModel { CommentID=item.Id, 
                                  AuthorDisplayName=item.Author.FirstName});
        }
        return View(vm);
      }
      return View("NotFound");
    }
    

    Now your view will be strongly typed to The PostViewModel

    @model PostViewModel
    
    <h2>@Model.PostText</h2>
    @Html.Partial("Comments",Model.Comments)
    

    And your partial view(Comments.cshtml) will be strongly typed to a collection of CommentViewModel

    @model List<CommentViewModel>
    @foreach(var item in Model)
    {
      <div>
         @item.Text
         <p>Written by @item.AuthorDisplayName</p>
      </div>
    }
    

    Now our views are not depending directly to Domain models. This allows us to bring data from another source tomorrow if we need (Ex :Get comments from a web service) and simply map to our view model.

    Some notes

    Do not add too much of code to Views. Let’s keep it pure HTML as much as possible. No data access calls directly from Views!

    I manually mapped the domain model to viewmodel for your understanding. You may use a mapping library like Automapper to do so. Also you may move part of the code we have in the GET action method to another servier layer so that it can be reused in multiple places.

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

Sidebar

Related Questions

I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
Seemingly simple, but I cannot find anything relevant on the web. What is the
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I have a small JavaScript validation script that validates inputs based on Regex. I
I am trying to render a haml file in a javascript response like so:
I am doing a simple coin flipping experiment for class that involves flipping a
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I know there's a lot of other questions out there that deal with this

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.