最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

c# - How to populate a View with 2 or more Entities? - Stack Overflow

programmeradmin0浏览0评论

I have a User entity

public class User
{
    public int UserID { get; set; } = 0;
    public string Name { get; set; } = string.Empty;
    public string Email { get; set; } = string.Empty;
    public string Phone { get; set; } =  string.Empty;
    public string UserStatus { get; set; } =  string.Empty;
}

and I have a Comment entity.

public class Comment
{
    public int CommentID { get; set; } = 0;
    public int ProductID { get; set; } = 0;
    public int UserID { get; set; } = 0;
    public string Text { get; set; } = string.Empty;
    public DateTime DateAdded { get; set; } = DateTime.MinValue;
}

I am wanting to get the following data on a view so when a user clicks a button it will display all the comments for a given ProductID like this:

My SQL query looks like this

SELECT CommentID,
    ProductID,
    C.UserID,
    U.Name,
    CommentText,
    DateAdded
FROM [Comment] C
JOIN [User] U ON C.UserID = U.UserID
WHERE ProductID = @ProductID

How to get the data I need on the view?

I have a User entity

public class User
{
    public int UserID { get; set; } = 0;
    public string Name { get; set; } = string.Empty;
    public string Email { get; set; } = string.Empty;
    public string Phone { get; set; } =  string.Empty;
    public string UserStatus { get; set; } =  string.Empty;
}

and I have a Comment entity.

public class Comment
{
    public int CommentID { get; set; } = 0;
    public int ProductID { get; set; } = 0;
    public int UserID { get; set; } = 0;
    public string Text { get; set; } = string.Empty;
    public DateTime DateAdded { get; set; } = DateTime.MinValue;
}

I am wanting to get the following data on a view so when a user clicks a button it will display all the comments for a given ProductID like this:

My SQL query looks like this

SELECT CommentID,
    ProductID,
    C.UserID,
    U.Name,
    CommentText,
    DateAdded
FROM [Comment] C
JOIN [User] U ON C.UserID = U.UserID
WHERE ProductID = @ProductID

How to get the data I need on the view?

Share Improve this question edited 1 hour ago marc_s 756k184 gold badges1.4k silver badges1.5k bronze badges asked 8 hours ago DaBeau96DaBeau96 5482 gold badges5 silver badges19 bronze badges 1
  • Either you can use ADO.NET to execute the query/stored procedure, or you convert the write as LINQ-to-SQL. Both examples can be founded in here. Would be great if you can do some attempts and show the error if you are stucked. Thanks. – Yong Shun Commented 2 hours ago
Add a comment  | 

1 Answer 1

Reset to default 0

wanting to get the following data on a view so when a user clicks a button it will display all the comments for a given ProductID

You can try to create a view model that represents the data and logic required by a view based on your requirements as below:

public class UserCommentsVM
{
    public int UserID { get; set; }
    public string Name { get; set; }
    //...
    //user basis info
    //...
    public List<Comment> Comments { get; set; }
}

And query records from database using your SQL query, construct and populate list of UserCommentsVM within your controller action.

var UserCommentsVMs = new List<UserCommentsVM>();
//...
//code logic here, query db and construct required view model
//...
return View(UserCommentsVMs);

In view page, specify the @model declaration to indicate that the view will be expecting several UserCommentsVM objects

@model IEnumerable<UserCommentsVM>
发布评论

评论列表(0)

  1. 暂无评论