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>
}
Ihre 'finalItem' Variable setzt nur Werte für' Blog' und 'Comment', nicht für' CommentDetails'. – sleeyuen
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? –
Ja, ich habe viele Kommentare zu einem Beitrag – damian17