2016-10-09 7 views
-3

私の小さなウェブサイトのサービスに問題があります。私はポストを壁にコードし、ユーザからのコメントをしなければならない。投稿の壁がいっぱいで、.NETでのコメント

public class Post 
{ 
    public int ID { get; set; } 
    public string PostContent { get; set; } 
    public DateTime PostDateTime { get; set; } 

    public virtual ApplicationUser ApplicationUser { get; set; } 
    public virtual ICollection<Comment> Comments { get; set; } 
} 

とコメントについて:私はビュー内のすべての記事、彼のコメントを表示することができますどのように

public class Comment 
{ 
    public int ID { get; set; } 
    public string CommentContent { get; set; } 
    public DateTime CommentDateTime { get; set; } 
    public int PostId { get; set; } 

    public virtual ApplicationUser ApplicationUser { get; set; } 
    public virtual Post Post { get; set; } 
} 

は、私はポストのためのいくつかのモデルがありますか?

答えて

-1

異なるモデルのものを混ぜ合わせる必要があるときは、通常ViewModelを使用します。

さまざまなソースまたは特定の計算のデータを配置する特定のビュー用のモデルです。

基本的には、フィールドが必要な別のモデルです。

あなたのケースでは、

public class PostAndComments 
{ 
    public int IDPost { get; set; } 
    public int IDComment { get; set; } 
    public string CommentContent { get; set; } 
    public DateTime CommentDateTime { get; set; } 
    public string PostContent { get; set; } 
    public DateTime PostDateTime { get; set; } 
    public virtual ApplicationUser ApplicationUser { get; set; } 
    public virtual Post Post { get; set; } 
} 

のようなものすることができ、あなたのアクションでは、両方から様々なフィールドをPostAndCommentし、ビューにそれを渡すためにASSING、ポストとコメントモデルの両方を通過します。

アクションは、それが部分図は、不必要な複雑さを追加しますthrought作る

ActionResult PostComment(Post post, Comment comment) 
{ 
    PostAndComments postcomment = new PostAndComments 
     { 
      IDPost = post.ID, 
      IDComment = comment.ID, 
      etc... etc... 
     } 
} 

のようなものでなければなりません。

+0

どのようにすべての投稿+コメントを1ページに表示するのですか?どのように見えるか?どのようにコントローラのように見えるのですか?あなたはあなたが投稿とコメントに対して別々のdivであなたのHTMLページを作成するだけででき モデルPostAndCommentsに合格しているCSHTMLページにアクセスしたらD – Zarobiek

+0

[OK]を、私はそれをしようとしますどこかでテンプレートをとる) Model.PostContentとModel.CommentContentあなたが必要な場所 これらのコードはすべて手前に@が必要ですので、RazorエンジンはC#コード – Zarobiek

+0

@Zarobiek(あるいは単に: –

関連する問題