2012-01-20 12 views
1

私はほぼ完成しました。コメントを追加する際に少し問題があります。コメントを追加するASP.NET MVC3のようなコメントをFacebookのように

"ギャラリー"と "コミットメント"を持つデータベースを作成しました。それから私はモデルを作成しました。

public class GalleryEntries 
{ 
    public IList<GalleryEntry> Entries { get; set; } 

    public GalleryEntries() 
    { 
     Entries = new List<GalleryEntry>(); 
    } 
} 

public class GalleryEntry 
{ 
    public Gallery GalleryImage { get; set; } 
    public List<Comment> Comments { get; set; } 
    public Comment Comment { get; set; } 
} 

私のコントローラは次のようになります。

GalleryDataContext GalleryDB = new GalleryDataContext(); 

    GalleryEntries galleryentries = new GalleryEntries(); 

    public ActionResult Index() 
    {  

     foreach (Gallery gallery in GalleryDB.Galleries) 
     { 
      GalleryEntry galleryentry = new GalleryEntry(); 
      galleryentry.Comments = GalleryDB.Comments.Where(c => c.BildID == gallery.ImageID).ToList(); 
      galleryentry.GalleryImage = gallery; 
      galleryentries.Entries.Add(galleryentry); 
     } 

     return View(galleryentries); 
    } 

    [HttpPost] 
    public ActionResult Index(Comment comment) 
    { 
     Comment newComment = new Comment(); 

     newComment.BildID = comment.BildID; 
     newComment.Comment1 = comment.Comment1; 

     GalleryDB.Comments.InsertOnSubmit(newComment); 
     GalleryDB.SubmitChanges(); 

     return RedirectToAction("Index"); 
    }  

最後に、ビュー..私はそれを適切なPictureIDを入力して、私のコメントを書いて提出する今

@model KK_Online.Models.GalleryEntries 

@foreach (var item in Model.Entries){ 

// here comes the picture and the written comments below.. it works perfectly.. 

//then down here I tried to create textarea.. 

@using (Html.BeginForm()) 
     { 
      @Html.ValidationSummary(true) 

     <div class="add_comment"> 
       <fieldset> 
        <legend> Add Comment </legend> 

        @Html.EditorFor(model => item.Comment.BildID) 
        @Html.ValidationMessageFor(model => item.Comment.BildID) 
        <br /> 

        @Html.TextAreaFor(model => item.Comment.Comment1) 
        @Html.ValidationMessageFor(model => item.Comment.Comment1) 

        <br /> 

        <button type="submit">Add Comment </button> 

       </fieldset> 

      </div> 

     } 
} 

、それは その "

The INSERT statement conflicted with the FOREIGN KEY constraint "FK_Comment_Gallery". The conflict occurred in database "PhotoComment", table "dbo.Gallery", column 'ImageID'. 
The statement has been terminated. 

"

表示します

誰でも私を助けてくれますか?

答えて

0

答えが見つかりました。

私たちはビュー内でこのクラスを処理しているので、単にCommentデータベースをGalleryentriesクラスに入れてください。

public class GalleryEntries 
{ 
    public IList<GalleryEntry> Entries { get; set; } 

    public GalleryEntries() 
    { 
     Entries = new List<GalleryEntry>(); 
    } 

    public Comment Comment {get; set;} 
} 
関連する問題