5
如何顯示MVC中頁面上多個表格的數據?如何顯示MVC頁面上多個表格的數據?
我的模型:
這是頁面模型
public class PageModel
{
[Key]
[DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
public int PageId { get; set; }
public string PageTitle { get; set; }
public string PageSubject { get; set; }
public DateTime CreateDate { get; set; }
public DateTime UpdateDate { get; set; }
public int ParentId { get; set; }
public bool IsActive { get; set; }
public IList<PageContentModel> PageContentModels { get; set; }
public IList<CommentModel> CommentModels { get; set; }
}
這是內容模型
public class PageContentModel
{
[Key]
[DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
public int ContentId { get; set; }
public int PageId { get; set; }
public virtual PageModel PageModel { get; set; }
[Required]
[DataType(DataType.MultilineText)]
[MaxLength(1024)]
public string Content { get; set; }
}
這個註釋模型
public class CommentModel
{
[Key]
[DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
public int CommentId { get; set; }
public string CommentAuthor { get; set; }
public string CommentContent { get; set; }
public DateTime CreateDate { get; set; }
public bool IsActive { get; set; }
public int PageId { get; set; }
public virtual PageModel PageModel { get; set; }
}
我寫了下面
但註冊評論
錯誤信息時,它提供一個錯誤信息:此對象
@model Tuple<DynamicWebsite.Models.PageModel, DynamicWebsite.Models.CommentModel>
@helper GetParentTitle(int id)
{
if (id != 0)
{
DynamicWebsite.DataLayer.EFDbContext x = new DynamicWebsite.DataLayer.EFDbContext();
var item = x.PageModels.Find(id);
x.Dispose();
@GetParentTitle(item.ParentId);
<span>| </span><a href="@item.PageId">@item.PageTitle</a>
}
else
{
@Html.ActionLink("Home", "Index", "Home")}
}
@GetParentTitle(Model.Item1.PageId)
@if (Model.Item1.IsActive)
{
ViewBag.Title = Model.Item1.PageTitle;
<fieldset>
<legend>PageModel</legend>
<div class="display-label">
Page Subject:<h2>@Model.Item1.PageSubject</h2>
</div>
<p>
@foreach (var item in Model.Item1.PageContentModels)
{
@item.Content
}
</p>
@if (Model.Item1.UpdateDate > Model.Item1.CreateDate)
{
<h5>Create Date: @String.Format("{0:g}", Model.Item1.CreateDate)</h5>
<h5>UpdateDate: @String.Format("{0:g}", Model.Item1.UpdateDate)</h5>
}
else
{
<h5>Create Date: @String.Format("{0:g}", Model.Item1.CreateDate)</h5>
}
@foreach (var item in Model.Item1.CommentModels)
{
if (item.IsActive)
{
<h3>@item.CommentAuthor said:</h3>
<p>@item.CommentContent</p>
<h5>Create Date: @String.Format("{0:g}", item.CreateDate)</h5>
}
}
</fieldset>
using (Html.BeginForm())
{
@Html.ValidationSummary(true)
<fieldset>
<legend>CommentModel</legend>
<div class="editor-label">
@Html.LabelFor(model => model.Item2.CommentAuthor)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Item2.CommentAuthor)
@Html.ValidationMessageFor(model => model.Item2.CommentAuthor)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Item2.CommentContent)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Item2.CommentContent)
@Html.ValidationMessageFor(model => model.Item2.CommentContent)
</div>
<p>
<input type="submit" value="Create" />
</p>
</fieldset>
}
}
else
{
ViewBag.Title = "Deactive";
<p>This page deactive.</p>
}
創建[視圖模型](http://lostechies.com/jimmybogard/2009/06/30/how-we-do-mvc-view-models/) – jrummell 2013-03-08 15:24:48
「註冊評論」是什麼意思?代碼中的錯誤發生在哪裏?哪行代碼拋出錯誤? – 2013-03-08 16:58:58
註冊評論:插入或添加用戶的新評論和錯誤點擊創建按鈕 – 2013-03-08 18:41:35