我要求在MVC上显示来自不同模型的1列。我是mvc的第一个计时器,所以我不熟悉lambda惯例...
到目前为止,我有这个课程
article.cs
public class Article
{
[Key]
public int bl_id { get; set; }
[Column("bl_title")]
public string Title { get; set; }
[Column("bl_img")]
public string Image { get; set; }
[Column("bl_summ")]
public string Summary { get; set; }
[AllowHtml]
[Column("bl_content")]
public string Content { get; set; }
[Column("bl_author")]
public string Author { get; set; }
[DisplayFormat(ApplyFormatInEditMode =true, DataFormatString ="{0:MM/dd/yyyy}")]
[Column("bl_dtecrt")]
public DateTime DateCreated { get; set; }
}
下一课是评论:
comments.cs
public class Comments
{
[Key]
public int ic_id { get; set; }
public int ic_blid { get; set; }
public string ic_comment { get; set; }
public DateTime ic_crtdte { get; set; }
public string ic_pcname { get; set; }
}
我需要的是将Articles.cs模型中的标题列与评论集相关联......如果它在sql上,我可以用这种方式进行选择查询:select article.title,comments.ic_comment来自文章,评论where articles.bl_id = comments.ic_blid
到目前为止,我尝试的解决方案是在控制器中创建一个连接,然后将其抛给视图...
articlescontroller.cs
public ActionResult Comments()
{
var comm = from c in db.Comments
join a in db.Articles
on c.ic_blid equals a.bl_id
select c;
return View("Comments", comm.ToList());
}
编辑:查看
Comments.cshtml
@model IEnumerable<WebApplication3.Models.Comments>
@{
ViewBag.Title = "Comments";
Layout = "~/Views/Shared/_Layout.cshtml";
}
@Html.DisplayNameFor(model => model.ic_comment)
</th>
<th>
@Html.DisplayNameFor(model => model.ic_crtdte)
</th>
<th>
@Html.DisplayNameFor(model => model.ic_pcname)
</th>
<th></th>
</tr>
@foreach (var item in Model) {
@Html.DisplayFor(ModelItem => item.Article.Title)
<tr>
<td>
@Html.DisplayFor(modelItem => item.ic_comment)
</td>
<td>
@Html.DisplayFor(modelItem => item.ic_crtdte)
</td>
<td>
@Html.DisplayFor(modelItem => item.ic_pcname)
</td>
<td>
@Html.ActionLink("Edit", "Edit", new { id=item.ic_id }) |
@Html.ActionLink("Details", "Details", new { id=item.ic_id }) |
@Html.ActionLink("Delete", "Delete", new { id=item.ic_id })
</td>
</tr>
}
</table>
但我认为不符合文章中的专栏。
所以我的问题是,如何将我的文章模型中的一个或多个列合并到我的评论模型中,然后在视图中显示它们?本单元背后的原因是维护发布的文章中的评论。
我尝试将其放在评论模型中:
public virtual Article Article { get; set; }
但我不知道如何使用。
我希望你能帮助我......
答案 0 :(得分:1)
您首先必须更正您的文章类。 由于文章可以包含许多注释,因此您需要一个描述相同
的属性(请注意代码未经过测试)
public class Article{
...
public List<Comment> Comments {get; set;}
...
}
现在您尝试检索数据的位置也包含注释。像这样:
public ActionResult Comments()
{
var comm = db.Article.Include(x=>x.Comments).ToList();
// This will get All article and include comments if any for each article.
return View("Comments", comm.ToList());
}
在您的视图中,您必须为单个文章
迭代评论@{
ViewBag.Title = "Comments";
Layout = "~/Views/Shared/_Layout.cshtml";
}
@foreach (var item in Model) {
@Html.DisplayFor(ModelItem => item.Article.Title)
<tr>
<td>
@Html.DisplayFor(modelItem => item.Comments.ic_comment)
</td>
<td>
@Html.DisplayFor(modelItem => item.Comments.ic_crtdte)
</td>
<td>
@Html.DisplayFor(modelItem => item.Comments.ic_pcname)
</td>
<td
<td>
@Html.ActionLink("Edit", "Edit", new { id=item.ic_id }) |
@Html.ActionLink("Details", "Details", new { id=item.ic_id }) |
@Html.ActionLink("Delete", "Delete", new { id=item.ic_id })
</td>
</tr>
}
答案 1 :(得分:0)
需要将View的Model声明为动态。请参阅 https://www.aspsnippets.com/Articles/Display-data-in-Single-View-from-Multiple-Tables-in-ASPNet-MVC.aspx 如果它有帮助。
同样修改如下
var comm = from c in db.Comments
join a in db.Articles
on c.ic_blid equals a.bl_id
select new {c,a.title};
答案 2 :(得分:0)
这可以通过以下2种方式完成。创建一个包含注释和标题的类。从此选择结果构造对象并将其传递给Model 或者在Article类中有一个Navigation属性用于注释。并修改LINQ-SQL,如下所示。
Artice课程
public class Article
{
[Key]
public int bl_id { get; set; }
[Column("bl_title")]
public string Title { get; set; }
[Column("bl_img")]
public string Image { get; set; }
[Column("bl_summ")]
public string Summary { get; set; }
[AllowHtml]
[Column("bl_content")]
public string Content { get; set; }
[Column("bl_author")]
public string Author { get; set; }
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:MM/dd/yyyy}")]
[Column("bl_dtecrt")]
public DateTime DateCreated { get; set; }
public virtual ICollection<Comments> Comments { get; set; }
}
查询 var comments1 =来自评论中的c 加入一篇文章 在c.ic_blid上等于a.bl_id 选择一个;
查看如下
@model IEnumerable<WebApplication1.Models.Article>
@{
ViewBag.Title = "Comments";
}
<h2>Comments</h2>
<p>
@Html.ActionLink("Create New", "Create")
</p>
<table class="table">
<tr>
<th>Title</th>
<th>ic_comment</th>
<th>ic_blid</th>
<th>ic_crtdte</th>
<th>ic_pcname</th>
</tr>
@foreach (var item1 in Model)
{
foreach (var item in item1.Comments)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item1.Title)
</td>
<td>
@Html.DisplayFor(modelItem => item.ic_comment)
</td>
<td>
@Html.DisplayFor(modelItem => item.ic_blid)
</td>
<td>
@Html.DisplayFor(modelItem => item.ic_crtdte)
</td>
<td>
@Html.DisplayFor(modelItem => item.ic_pcname)
</td>
<td>
@Html.ActionLink("Edit", "Edit", new { id=item.ic_id }) |
@Html.ActionLink("Details", "Details", new { id=item.ic_id }) |
@Html.ActionLink("Delete", "Delete", new { id=item.ic_id })
</td>
</tr>
}
}
</table>
答案 3 :(得分:0)
@Jja的答案是接受的答案,但因为我在导航属性方面遇到了麻烦......
根据此stackoverflow链接:The Name value should be a valid navigation property name.
使用ForeignKey时出现问题,您连接的虚拟表必须在[ForeignKey(“”)]标记上具有相同的名称。之前,我的评论课被定义为:
[Table("Intranet_Comments")]
public class Intranet_Comments
{
[Key]
public int ic_id { get; set; }
[ForeignKey("Intranet_Article")]
public int ic_blid { get; set; }
public string ic_comment { get; set; }
public DateTime ic_crtdte { get; set; }
public string ic_pcname { get; set; }
public virtual Article Article { get; set; }
}
它会导致导航错误......我重写了这个课程:
[表( “Intranet_Comments”)] 公共类Intranet_Comments
{
[Key]
public int ic_id { get; set; }
[ForeignKey("Article")]
public int ic_blid { get; set; }
public string ic_comment { get; set; }
public DateTime ic_crtdte { get; set; }
public string ic_pcname { get; set; }
public virtual Article Article { get; set; }
}
外键属性名称必须与虚拟属性集名称相同。
我希望这有帮助!