1

I'm trying to get Product data with its ProductComments but I'm getting null error when I ask for it. There is a one-to-many relation between Product and ProductComments.

Let me show you my data access layer:

public interface IRepositoryProduct<T> : IRepository<Product>
{
    List<Product> GetProductWithComment();
}

public class RepositoryProduct<T> : Repository<Product>, IRepositoryProduct<T>
{
    public RepositoryProduct(TradeTurkDBContext context) : base(context) { }

    public List<Product> GetProductWithComment()
    {
        return TradeTurkDBContext.Products.Include(x => x.ProductComment).ToList();
    }
}

And this is my view model class:

public class ProductCommentViewModel
{
    public Product Product { get; set; }
    public List<Product > ProductsC { get; set; }
}

and here is my controller:

var ListProductComment = _unitOfWorkP.RepositoryProduct.GetProductWithComment();
var Product= new Product();
var ProductCommentViewModel = new ProductCommentViewModel
        {
            Product= Product,
            ProductsC= ListProductComment
        };
return View(ProductCommentViewModel);

When I check out if there is a data inside of Product, it tells me there is a 2 comment inside of ProductComments.

Let me show you how I am trying to reach that data in cshtml:

@foreach (var item in Model.ProductsC)
{
    <tr>
        <td class="text-center">
            <div class="dropdown custom-dropdown">
            <a class="dropdown-toggle" href="#" role="button" id="dropdownMenuLink1" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
            <svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"
                                             class="feather feather-more-horizontal">
                <circle cx="12" cy="12" r="1"></circle>
                <circle cx="19" cy="12" r="1"></circle>
                <circle cx="5" cy="12" r="1"></circle>
            </svg>
            </a>
            <div class="dropdown-menu" aria-labelledby="dropdownMenuLink1" style="will-change: transform;">
                <!-- SONRA YAPILACAK <a class="dropdown-item" href="user_profile.php?userID=">Görüntüle</a>-->
                // this is the point where I get error  
                <a id="@item.ProductComment.CommentID" class="dropdown-item" data-toggle="modal" data-target="#productCommentUpdate-@item.ProductComment.CommentID">Güncelle</a>
                <a id="@item.ProductComment.CommentID" class="dropdown-item" data-toggle="modal" data-target="#productCommentDelete-@item.ProductComment.CommentID">Sil</a>
            </div>
        </div>
    </td>
    <td>@item.ProductComment.CommentID</td>
    <td>@item.ProductComment.ShoppingUserID</td>
    <td>@item.ProductComment.ShoppingUserID</td>
    <td>@item.ProductID</td>
    <td><button class="btn btn-outline-dark mb-2" data-toggle="modal" data-target="#productCommentView-@item.ProductComment.CommentID">Yorumu Görüntüle</button></td>
    <td>@item.ProductComment.CreatedUserId</td>
    <td>@item.ProductComment.ModifiedUserId</td>
    <td>@item.ProductComment.CreatedUserType</td>
    <td>@item.ProductComment.ModifiedUserType</td>
    <td>@item.ProductComment.CreatedDate</td>
    <td>@item.ProductComment.ModifiedDate</td>
    <td>@item.ProductComment.ProductCommentIsActive</td>
    <td>@item.ProductComment.IsDeleted</td>
</tr>
}

Thanks for any suggestion !

i edit my question but im still gettin that error

BerkGarip
  • 534
  • 5
  • 18
  • your `ProductComment` has a reference called `ProductComment`? that sounds like comment of comment? but your loading shows that you load only comment of product (not comment of comment). So that's why it's null. – King King Mar 19 '21 at 16:22
  • Nope its for ```Product```. there is a no comment of comment in my project. @KingKing – BerkGarip Mar 19 '21 at 16:44
  • so it's confusing, from your code the `item` should be a `ProductComment` (item of `List`), then inside the loop, you access like this `@item.ProductComment.CommentID`. So it has `ProductComment` property. If you mean it's not a `ProductComment` but a `Product`, it's still very confusing about its name as well as the exposed property `CommentID` on a product. Everything is confusing now. – King King Mar 19 '21 at 16:49
  • so u are suggesting to make a another foreach inside that existing foreach to reach out that ```ProductComment``` right ? – BerkGarip Mar 19 '21 at 16:54
  • No I'm not suggesting anything, I just feel confused about your models design. As pointed out clearly in my comments. – King King Mar 19 '21 at 16:59
  • i understand your point now. my model has ``` public List ProductsC { get; set; }``` but im trying to get that data on ```IRepositoryProdudct``` . it should be ``` public List ProductsC { get; set; }```. i will edit my question,stay tune – BerkGarip Mar 20 '21 at 09:21
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/230155/discussion-between-berkgarip-and-king-king). – BerkGarip Mar 20 '21 at 09:25
  • actually you've just moved the center of your mess from the view out to the view model. It made sense about the view model first but not the view, now it makes sense about the view but not the view model. Actually I guess your original view model is correct (`ProductsC` is a `List`). Your view tries to render ***all comments*** on a ***specific product***. So you need correct data returned from `GetProductWithComment()` (which should be `GetProductComments(int productId)` to make sense). You have both problems of getting & rendering data - there are a lot to solve here. – King King Mar 20 '21 at 09:45
  • i know there is should be a ```Where(x=>x.ProductID==ID)``` statement like that. im just trying to find out can i display my data ``` @item.ProductComment.CreatedUserId````like that or not. is it possible,if its possible how :( – BerkGarip Mar 20 '21 at 09:49
  • 1
    if corrected to the original view model, your product info is accessed via `Model.Product` and ***each comment*** is accessed via `item` (meaning `item` is actually an instance of `ProductComment`), so there is no access like `item.ProductComment`. You get all the comment info via `item`. – King King Mar 20 '21 at 09:51
  • is there a alternative way ? because i dont wanna create a second loop like ```@foreach (var comment in item) {``` inside of that existing loop – BerkGarip Mar 20 '21 at 10:07
  • you just need one loop, no second loop here. In each loop you render a comment for the product. So that's required. Here's for the learning purpose, that's what you can do. But in product, we should not load all the comments like that, the client should load one page of comments (for one time requested by user), with better UX thanks to the support of ajax requests. – King King Mar 20 '21 at 10:10
  • i get what u mean. as u explain,its much better. thanks for keeping up your effort for me !!! im a junior developer and you just create a new vision on me – BerkGarip Mar 20 '21 at 10:15
  • 1
    you're welcome, just keep learning and you will surely be better. – King King Mar 20 '21 at 10:18

0 Answers0