我正在尝试Product
使用它获取数据,ProductComments
但是当我要求它时出现空错误。Product
和之间存在一对多的关系ProductComments
。
让我向您展示我的数据访问层:
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();
}
}
这是我的视图模型类:
public class ProductCommentViewModel
{
public Product Product { get; set; }
public List<Product > ProductsC { get; set; }
}
这是我的控制器:
var ListProductComment = _unitOfWorkP.RepositoryProduct.GetProductWithComment();
var Product= new Product();
var ProductCommentViewModel = new ProductCommentViewModel
{
Product= Product,
ProductsC= ListProductComment
};
return View(ProductCommentViewModel);
当我检查里面是否有数据时Product
,它告诉我里面有 2 条评论ProductComments
。
让我向您展示我是如何尝试获取这些数据的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>
}
感谢您的任何建议!
我编辑了我的问题,但我仍然遇到那个错误