我正在想办法做这个简单的博客。我有一个首页,其中列出了所有博客文章,当您单击博客文章的标题时,您可以输入整个文章,您也可以在其中发表评论。
我的问题是,当我尝试通过控制器传递模型时,我只能访问我的一个模型,无论是帖子还是评论。
这是我的模型:
public class Post
{
public int ID { get; set; }
public string Title { get; set; }
public string Message { get; set; }
public string Author { get; set; }
public DateTime DateTime { get; set; }
public List<Comment> Comments { get; set; }
}
public class Comment
{
public int ID { get; set; }
public int PostID { get; set; }
public string Message { get; set; }
public string Author { get; set; }
public DateTime DateTime { get; set; }
}
这是我的控制器:
public ActionResult Details(int id)
{
**var dbPosts = db.Posts.Find(id);**
return View(dbPosts);
}
这是我的看法:
@model Blog.Models.Post
@{
ViewBag.Title = "Details";
}
<h2>@Html.ActionLink(Model.Title, "Details", new { id = Model.ID (@Html.ActionLink("Rediger", "Edit", new { id = Model.ID }) - @Html.ActionLink("Slet", "Delete", new { id = Model.ID }))</h2>
<span class="written">skrevet d. @Model.DateTime.ToLongDateString() @Model.DateTime.ToShortTimeString() af @Model.Author</span>
<p>@Model.Message</p>
<hr />
@foreach (var comment in Model.Comments) {
<span class="written">skrevet d. @comment.DateTime.ToLongDateString() @comment.DateTime.ToShortTimeString() af @comment.Author</span>
<p>@comment.Message</p>
<hr />
}
@using (Html.BeginForm()) {
<label for="Author">Author</label>
<input type="text" id="Author" name="Author" />
<label for="Message">Message</label>
<textarea id="Message" name="Message"></textarea>
<input type="submit" value="Gem" />
<input type="reset" value="Reset" />
}
我用星号标记的地方是我认为出错的地方。我需要让我的帖子和评论模型都通过此详细信息视图。我的问题是,我该怎么做?
我不想让任何先进的东西只是想学习。
提前致以亲切的问候和感谢。