我希望创建一个类似下图的在线论坛:
这是我的控制器方法:
public ActionResult Discussion_Preview()
{
int Discussion_ID = 1;
var discussion_preview = (from d in db.AB_Discussion
where d.Discussion_ID == Discussion_ID
join dc in db.AB_DiscussionComments on d.Discussion_ID equals dc.Discussion_ID
join user_discussion in db.AspNetUsers on d.CreatedBy equals user_discussion.Id
join user_comments in db.AspNetUsers on dc.CreatedBy equals user_comments.Id
select new DiscussionPreview_Model
{
Disussion_ID = d.Discussion_ID,
Discussion_CreateDate = d.CreatedDate,
Discussion_CreateBy = user_discussion.UserName,
Discussion_Title = d.Discussion_Name,
Discussion_Description = d.Discription,
Comment_ID = dc.Comment_ID,
Comment_Description = dc.Comment_Discription,
Comment_CreateDate = dc.CreatedDate,
Comment_CreateBy = user_comments.UserName
});
return View(discussion_preview);
}
模型类
public class DiscussionPreview_Model
{
public int Disussion_ID { get; set; }
public Nullable<System.DateTime> Discussion_CreateDate { get; set; }
public string Discussion_CreateBy { get; set; }
public string Discussion_Title { get; set; }
public string Discussion_Description { get; set; }
public int Comment_ID { get; set; }
public Nullable<System.DateTime> Comment_CreateDate { get; set; }
public string Comment_CreateBy { get; set; }
public string Comment_Description { get; set; }
}
查看页面
@model IEnumerable<prjct.Models.DiscussionPreview_Model>
@{
ViewBag.Title = "Discussion_Preview";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<h3>@Html.DisplayFor(model => model.Discussion_Title)</h3>
<div>@Html.DisplayFor(model => model.Discussion_Description)</div>
<div>@Html.DisplayFor(model => model.Discussion_CreateDate) @Html.DisplayFor(model => model.Discussion_CreateDate)</div>
@foreach (var item in Model)
{
<fieldset>
<legend></legend>
<h4>Comments</h4>
<div class="display-field">@item.Comment_Description</div>
<div class="display-field">@item.Comment_CreateBy : @item.Comment_CreateDate </div>
</fieldset>
}
当我调试上述应用程序时,我收到以下错误消息: “IEnumerable”不包含“Discussion_Title”的定义,也找不到接受类型为“IEnumerable”的第一个参数的扩展方法“Discussion_Title”(是否缺少使用指令或程序集引用?)
DisplayFor()
辅助方法需要在foreach
循环内部。但我最好的猜测是,你真正想做的是显示一个DiscussionPreview_Model
和与之关联的所有评论集合? - user3559349IEnumerable
就代表这个含义)。 - user3559349Title
、Description
等属性,以及IEnumerable<Comment> Comments
,其中Comment
包含CreateBy
和Description
等属性。 - user3559349ID
而不是Disussion_ID
:) - user3559349