如何在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>
}
答案 0 :(得分:1)
创建新模型错误更正
public class PageViewModel
{
public PageModel PageModel { set; get; }
public CommentModel CommentModel { set; get; }
}