我試圖找出一種方法來做到這一簡單的博客。我有一個首頁,其中列出了所有的博客文章,當您點擊博客文章的標題時,您可以輸入您可以發表評論的整篇文章。MVC:通過控制器查看多個模型
我的問題是,當我嘗試通過控制器傳遞模型時,我只能訪問我的模型之一,無論是帖子還是評論。
這裏是我的模型:
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" />
}
我打上星的事情是,我覺得它出錯了..我需要讓我的發佈和評論模型通過這個詳細信息視圖。我的問題是,我該怎麼做?
我並沒有試圖讓任何先進的東西試圖學習。
提前致以親切的問候和感謝。
是否是文章和評論POCO實體?你在用什麼ORM? – Maess 2013-04-09 12:45:38