我想创建如下图像的在线论坛
这是我的控制器方法
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>
}
在调试上面的应用程序时,我收到以下错误消息
&#39; IEnumerable的&#39;不包含&#39; Discussion_Title&#39;的定义没有扩展方法&#39; Discussion_Title&#39;接受类型&#39; IEnumerable&#39;的第一个参数。可以找到(你错过了使用指令或程序集引用吗?)
答案 0 :(得分:1)
您正在尝试使用
<h3>@Html.DisplayFor(model => model.Discussion_Title)</h3>
但是这里你的模型是IEnumerable,所以你有两个选择。
如果您确定所有记录的Discussion_Title,Discussion_Description和Discussion_CreateDate相同,那么您必须使用
<h3>@Html.DisplayFor(model => model.ElementAt<DiscussionPreview_Model>(0).Discussion_Title)</h3>
和其他2列相同,但在这种情况下,必须至少有一条记录,否则你必须在使用之前检查记录的数量。
尝试以类似
的方式创建模型public class Discssion
{
public string Discussion_Title { get; set; }
public string Discussion_Description { get; set; }
public string Comment_CreateBy { get; set; }
public List<DiscussionPreview> DiscussionPreviews{ get;set;}
}
public class DiscussionPreview
{
public class Comment_Description{ get; set; }
.....
}
并在视图中以相同的方式使用它。
请告诉我您需要更多详情。