alireza_rashvand
جمعه 27 تیر 1393, 17:11 عصر
با سلام
برای نمایش نظرهای یک پست, از ViewModel استفاده کردم:
public class PostCommentsViewModel
{
private int? Id;
public Post Post { get; set; }
public List<Comment> Comments { get; set; }
public PostCommentsViewModel(int? Id)
{
var db = new Context();
Post = db.Posts.First(x => x.Id == Id);
var query = from x in db.Comments where x.PostId == Id select x;
Comments = query.ToList();
}
}
برای نمایش ان هم در کنترلر مربوط :
public ActionResult Show(int? Id)
{
var viewModel = new PostCommentsViewModel(Id);
return View(viewModel);
}
کلاس پست:
public class Post
{
[Key]
public int Id { get; set; }
[DisplayName("عنوان خبر :")]
public String Title { get; set; }
[AllowHtml]
[DisplayName("متن خبر :")]
public String Content { get; set; }
public DateTime DateCreated{ get; set; }
public Boolean Show { get; set; }
public int UserId { get; set; }
public virtual ICollection<Comment> Comments { get; set; }
}
کاربر رو به ان اضافه نکردم برای همین فعلا به صورت دستی UserId ثبت می کنم.
کلاس نظر:
public class Comment
{
public int Id { get; set; }
public String Name { get; set; }
public String Content { get; set; }
public DateTime DateCreated { get; set; }
[ForeignKey("PostId")]
public virtual Post Post { get; set; }
public int PostId { set; get; }
}
ولی متاسفانه خطای زیر را می دهد.
The model item passed into the dictionary is of type 'Projects.ViewModels.PostCommentsViewModel', but this dictionary requires a model item of type 'Projects.Models.Post'.
ایا کد یا روشم اشتباه است؟ دوستان چه پی شنهادی دارند؟
برای نمایش نظرهای یک پست, از ViewModel استفاده کردم:
public class PostCommentsViewModel
{
private int? Id;
public Post Post { get; set; }
public List<Comment> Comments { get; set; }
public PostCommentsViewModel(int? Id)
{
var db = new Context();
Post = db.Posts.First(x => x.Id == Id);
var query = from x in db.Comments where x.PostId == Id select x;
Comments = query.ToList();
}
}
برای نمایش ان هم در کنترلر مربوط :
public ActionResult Show(int? Id)
{
var viewModel = new PostCommentsViewModel(Id);
return View(viewModel);
}
کلاس پست:
public class Post
{
[Key]
public int Id { get; set; }
[DisplayName("عنوان خبر :")]
public String Title { get; set; }
[AllowHtml]
[DisplayName("متن خبر :")]
public String Content { get; set; }
public DateTime DateCreated{ get; set; }
public Boolean Show { get; set; }
public int UserId { get; set; }
public virtual ICollection<Comment> Comments { get; set; }
}
کاربر رو به ان اضافه نکردم برای همین فعلا به صورت دستی UserId ثبت می کنم.
کلاس نظر:
public class Comment
{
public int Id { get; set; }
public String Name { get; set; }
public String Content { get; set; }
public DateTime DateCreated { get; set; }
[ForeignKey("PostId")]
public virtual Post Post { get; set; }
public int PostId { set; get; }
}
ولی متاسفانه خطای زیر را می دهد.
The model item passed into the dictionary is of type 'Projects.ViewModels.PostCommentsViewModel', but this dictionary requires a model item of type 'Projects.Models.Post'.
ایا کد یا روشم اشتباه است؟ دوستان چه پی شنهادی دارند؟