关于mvc4多表查询并在前台显示
1.首先是创建一个实体:class xxmodel{
public string username{get;set;}
public string articlename{get;set;}
public int articleid{get;set;}
}
2.controller:
public actionresult index(){
var query=from d in user join a in article on d.userid equals a.userid select new xxmodel{
username=d.username,
articlename=a.articlename,
articleid=a.article
};
return view(query.toList());
}
3.view:创建视图
@model PagedList.IPagedList<dell.Models.userInfor>
@{
ViewBag.Title = "Index";
}
foreach(var item in model){
<tr>
<td>
@Html.DisplayFor(modelItem => item.username)
</td>
<td>
@Html.DisplayFor(modelItem => item.articlename)
</td>
<td>
@Html.DisplayFor(modelItem => item.articleid)
</td>
</tr>
}
<div>
Page @(Model.PageCount < Model.PageNumber ? 0 : Model.PageNumber)
of @Model.PageCount
@if (Model.HasPreviousPage)
{
@Html.ActionLink("<<", "select", new { page = 1})
@Html.Raw(" ");
@Html.ActionLink("< Prev", "select", new { page = Model.PageNumber - 1})
}
else
{
@:<<
@Html.Raw(" ");
@:< Prev
}
@if (Model.HasNextPage)
{
@Html.ActionLink("Next >", "select", new { page = Model.PageNumber + 1})
@Html.Raw(" ");
@Html.ActionLink(">>", "select", new { page = Model.PageCount })
}
else
{
@:Next >
@Html.Raw(" ")
@:>>
}
</div>
写完收工!!!欢迎大家指正。
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步