2017-05-23 3 views
0

Ich möchte zwei Modelle in einer Ansicht hinzufügen: CommentVM und BlogVM. CommentVM - Kommentare, BlogVM - PostDetails. Ich versuche, einen Kommentar über Ajax zu meiner Datenbank hinzuzufügen und dann mein zwei Modell in Sicht zu übergeben. Aber wenn ich versuche, meine Seite anzuzeigen, erhalte ich den Fehler, dass mein Objekt null ist (commentVM) unter Code meines Controllers Irgendwelche suggestion, was ich falsch mache? Vielen Dank für Ihre Hilfe!Kommentar hinzufügen - zwei Modelle in einer Ansicht

// GET: Admin/Blog/kategoria/{name}/post/id 
    [ActionName("post")] 
    public ActionResult PostDetails(int id) 
    { 
     //Declare BlogVM 
      BlogVM model; 
     CommentVM model2; 

     int id2; 

      using (Db db = new Db()) 
      { 
       //Get the page 
       BlogDTO dto = db.Blog.Find(id); 


       //Confirm page exist 
       if (dto == null) 
       { 
        return Content("Taka strona nie istnieje!"); 
       } 

       //Init BlogVM 
       model = new BlogVM(dto); 

      id2 = dto.Id; 
      // CommentDTO dto2 = db.Comments.Find(x => x.PostId == id2); 

      model2 = new CommentVM(); 
     } 

      var finalItem = new DetailsComment 
      { 
       Blog = model, 
       Comment = model2 
      }; 

     return View("PostDetails", finalItem); 

    } 

Code meine Modelle:

public class CommentVM 
{ 
    public CommentVM() 
    { 

    } 

    public CommentVM(CommentDTO row) 
    { 
     Id = row.Id; 
     Name = row.Name; 
     Body = row.Body; 
     PostId = row.PostId; 
     CreatedAt = row.CreatedAt; 
    } 

    public int Id { get; set; } 
    [Required] 
    [StringLength(50, MinimumLength = 3)] 
    public string Name { get; set; } 
    [Required] 
    [StringLength(50, MinimumLength = 3)] 
    public string Body { get; set; } 
    public int PostId { get; set; } 
    public DateTime CreatedAt { get; set; } 

    //public IEnumerable<CommentVM> CommentDetails { get; set; } 
} 



public class BlogVM 
{ 
    public BlogVM() 
    { 

    } 

    public BlogVM(BlogDTO row) 
    { 
     Id = row.Id; 
     Title = row.Title; 
     Slug = row.Slug; 
     Body = row.Body; 
     CategoryName = row.CategoryName; 
     CategoryId = row.CategoryId; 
     CreatedAt = row.CreatedAt; 
     Sorting = row.Sorting; 
     HasSidebar = row.HasSidebar; 
    } 

    public int Id { get; set; } 
    [Required] 
    [StringLength(50, MinimumLength = 3)] 
    public string Title { get; set; } 
    public string Slug { get; set; } 
    [Required] 
    [StringLength(int.MaxValue, MinimumLength = 3)] 
    [AllowHtml] 
    public string Body { get; set; } 
    public string CategoryName { get; set; } 
    [Required] 
    public int CategoryId { get; set; } 
    public DateTime CreatedAt { get; set; } 
    public int Sorting { get; set; } 
    public bool HasSidebar { get; set; } 

    public IEnumerable<SelectListItem> Categories { get; set; } 
} 





public class DetailsComment 
{ 
    public BlogVM Blog { get; set; } 
    public CommentVM Comment { get; set; } 

    public IEnumerable<CommentVM> CommentDetails { get; set; } 
} 

Meiner Ansicht

@foreach (var item in Model.CommentDetails) 
       { 

        <tr> 
         <td> 
          <div class="ajaxdivtd"></div> 
          @Html.DisplayFor(modelItem => item.Name) 
         </td> 
         <td> 
          @Html.DisplayFor(modelItem => item.Body) 
         </td> 
         <td> 
          @Html.DisplayFor(modelItem => item.CreatedAt) 
         </td> 
        </tr> 
       } 
+1

Ihre 'finalItem' Variable setzt nur Werte für' Blog' und 'Comment', nicht für' CommentDetails'. – sleeyuen

+0

Warum haben Sie ein CommentVM-Objekt und eine CommentVM-Liste innerhalb der Klasse "DetailsComment"? Von dem, was ich verstanden habe, wirst du eine Menge Kommentare für einen bestimmten Beitrag haben, ist das richtig? –

+0

Ja, ich habe viele Kommentare zu einem Beitrag – damian17

Antwort

0

Wenn nach Ihre Kommentare, Sie mehrere Kommentare per Post, dann ist hier, was Sie tun müssen:

//Declare BlogVM 
BlogVM model; 
CommentVM model2; //you can probably get rid of this variable since you want multiple comments per post, rather than a single one, but that's up to you 
List<CommentVM> commentsModel; //new List object to hold the multiple comments for the post 

und:

id2 = dto.Id; 
var dto2 = db.Comments.Find(x => x.PostId == id2); 
//Convert dto2 from an list/enumerable of CommentDTO to a List<CommentVM>, perhaps via AutoMapper (or other such utility), or even manually via a foreach 
commentsModel = YourConversionMethod(dto2); 

schließlich:

var finalItem = new DetailsComment 
{ 
    Blog = model, 
    Comment = model2, 
    CommentDetails = commentsModel 
}; 
+0

var dto2 = db.Comments.Find (x => x.PostId == id2); In dieser Zeile erhalte ich FEHLER: Kann Lambda-Ausdruck nicht in Objekttyp konvertieren, weil es kein Delegattyp ist – damian17

+0

OK, dann erlaubt Ihre 'Find'-Methode nur Objekte und ist die falsche Methode, um mehrere Datensätze abzurufen. Ohne mehr über Ihr Setup zu wissen, ist es schwierig zu sagen, welche Methode zur Ausführung dieser Funktion geeignet ist. In unserem Setup haben wir ein "IQueryable " -Objekt, das es uns ermöglicht, '.Where' mit Filtern wie' x => x.PostId == id2' zu verwenden. – sleeyuen

+0

Danke für Ihre Hilfe! Ich habe das gemacht, aber es hat nicht funktioniert. Vielleicht weil ich neu in ASP bin. Ich möchte nur in meiner Anwendung zwei Modelle aus meiner Sicht weitergeben: eines mit Post-Details (BlogVM funktioniert) und eines mit Kommentardaten (CommentVM funktioniert nicht). Wenn Sie einen Vorschlag oder ein Beispiel haben, lassen Sie es mich bitte wissen. – damian17

Verwandte Themen