سلام من 2 موجودیت دارم که رابطه چند به چند دارند
موجودیت مطلب و موضوعات
هر مطلب می تواند چند موضوع داشته باشد (در چند موضوع باشد) و هر موضوع هم می تولند چند مطلب داشته باشند
public class BlogPost { public long BlogPostId { get; set; } public string Subject { get; set; } public string Content { get; set; } public string PostImage { get; set; } public DateTime InsertedDate { get; set; } [ForeignKey("BlogCategories")] public long BlogCategoryId { get; set; } public virtual ICollection<BlogCategory> BlogCategories{ get; set; } public byte UserId { get; set; } [ForeignKey("UserId")] public virtual User User { get; set; } }
و موجودیت موضوعات
public class BlogCategory { public long BlogCategoryId { get; set; } public long? BlogCategoryParentId { get; set; } public string Title { get; set; } [ForeignKey("BlogPosts")] public long BlogPostId { get; set; } public virtual ICollection<BlogPost> BlogPosts { get; set; } }
حالا مشکلی که من دارم این هست که وقتی در ویو می خواهم زیر پست در موضوعاتی که پست در آنها ثبت شده را نمایش بدهم مقدار را 0 بر میگرداند
@foreach (var item in Model.BlogCategories) { <label>@item.Title</label> }
و همچنین به صورت مستقیم هم به پراپرتی ها دسترسی ندارم این طوری:
@Model.BlogCategories.Title
چطور میتوانم از طریق navigation property ها به موجودیتی که مرتبط هست دسترسی پیدا کنم ؟
برای مدل ها یک سازنده خالی قرار بدید
مثال
public class BlogCategory { public BlogCategory() { } public long BlogCategoryId { get; set; } public long? BlogCategoryParentId { get; set; } public string Title { get; set; } [ForeignKey("BlogPosts")] public long BlogPostId { get; set; } public virtual ICollection<BlogPost> BlogPosts { get; set; } }
ممنون ولی نمی دانم چرا هنوز مشکل پابرجاست
حتی من کد ها را به این صورت تغییر دادم ولی باز نشد
public class BlogPost { public BlogPost() { BlogCategories = new HashSet<BlogCategory>(); } public long BlogPostId { get; set; } public string Subject { get; set; } public string Content { get; set; } public string PostImage { get; set; } public DateTime InsertedDate { get; set; } [ForeignKey("BlogCategories")] public long BlogCategoryId { get; set; } public virtual ICollection<BlogCategory> BlogCategories{ get; set; } public byte UserId { get; set; } [ForeignKey("UserId")] public virtual User User { get; set; } }
و
public class BlogCategory { public BlogCategory() { BlogPosts=new HashSet<BlogPost>(); } public long BlogCategoryId { get; set; } public long? BlogCategoryParentId { get; set; } public string Title { get; set; } [ForeignKey("BlogPosts")] public long BlogPostId { get; set; } public virtual ICollection<BlogPost> BlogPosts { get; set; } }
متن ارور را ضمیمه کنید
در ویو وقتی اینطوری مینویسم اصلا Title را نمی شناسد
@item.BlogCategories.Title
و خطای زیر را میدهد
Severity Code Description Project File Line Suppression State
Error CS1061 'ICollection<BlogCategory>' does not contain a definition for 'Title' and no extension method 'Title' accepting a first argument of type 'ICollection<BlogCategory>' could be found (are you missing a using directive or an assembly reference?)
و وقتی هم به این صورت مینویسم
@foreach (var item2 in item.BlogCategories) { <label class="alert-warning">@item2.Title</label> }
item.BlogCategories
این قسمت 0 می باشد !
کل ویو
@model ICollection<DomainClassesLayer.BlogPost> @foreach (var item in Model) { <div class="col-md-4 grid-item"> <div class="panel panel-default"> <div class="panel-heading"> <strong class="post-title">@item.Subject</strong> </div> <div class="panel-body"> <a href="@Url.Action("Post","Post",new {area=""})/@item.BlogPostId" style="text-decoration: none"> <div class="col-md-6"> <img src="@(item.PostImage ?? "/Upload/BlogPostImg/NoImage.png")" alt="@item.Subject" class="img-responsive img-thumbnail" /> </div> </a> </div> <div class="panel-footer"> <label>ارسال توسط</label> <strong>@item.User.DisplayName</strong> <span class="glyphicon glyphicon-user"></span> | <label>در تاریخ </label> <strong>@item.InsertedDate</strong> <span class="glyphicon glyphicon-time"></span> | @item.BlogCategories.Title @foreach (var item2 in item.BlogCategories) { <label class="alert-warning">@item2.Title</label> } </div> </div> </div> }
متاسفانه نتونستم خطا رو پیدا کنم
باید کدهاتون رو مجدد بررسی کنید ، به احتمال زیاد جایی خطایی وجود داره یا چیزی رو فراموش کردید
هیچ کاربری تا کنون از این پست تشکر نکرده است
با ما تماس بگیرید تا در این مسیر همراهتان باشیم :)