admin管理员组文章数量:1366123
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
1 Answer
Reset to default 0wanting 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>
本文标签: cHow to populate a View with 2 or more EntitiesStack Overflow
版权声明:本文标题:c# - How to populate a View with 2 or more Entities? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1743745625a2531701.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论