C# 根据二级栏目Id查询出所属文章的信息列表

这里和单纯查询工能不同,这里是根据父级ID查询出子级的信息

Layout View部分

@using Droplets.Models;
@{
Layout = null;
List<LanMu2>
lanmu2 = ViewBag.LanMu2Name;
}

 

<!DOCTYPE html>

<html>
<head>
<meta name="viewport" content="width=device-width" />
<link href="~/Content/easyui.css" rel="stylesheet" />
<link href="~/Content/icon.css" rel="stylesheet" />
<script src="~/Scripts/jquery-1.8.2.min.js"></script>
<script src="~/Scripts/jquery.easyui.min.js"></script>
<script src="~/Scripts/easyui-lang-zh_CN.js"></script>
<title>Index</title>
<script type="text/javascript">
//function lanmu() {
// $('#content').attr('src','/Admin/Index');
//}

function wenzhang(id) {
$('#content').attr('src', '/WenZhang/Index?id='+id);
}
</script>
<style type="text/css">
.Ding {
height: 210px;
background-image: url('/Content/background_1/3.jpg');
}
</style>

</head>
<body class="easyui-layout">
<div class="Ding" data-options="region:'north',split:true" style="height:120px;"><h1>网站后台管理系统</h1></div>
<div data-options="region:'west',title:'导航菜单',split:true" style="150px;">
<ul class="easyui-tree">
@if (lanmu2 != null)
{

foreach (LanMu2 item1 in lanmu2)
{

<li>
<a onclick="wenzhang('@item1.Id')">@item1.Name</a>
</li>
}
}
<li>修改密码</li>
<li>退出</li>
</ul>
</div>
<div data-options="region:'center',title:'内容'" style="padding:5px;background:#eee;">
<iframe id="content" style="100%;height:98%"></iframe>
</div>

</body>
</html>

ManagementController部分:

public class ManagementController : AppController
{
//
// GET: /Management/
Droplets.Models.DropletsEntities db;

public ManagementController()
{
db = new DropletsEntities();
}

protected override void Dispose(bool disposing)
{
if (disposing)
{
db.Dispose();
}
base.Dispose(disposing);
}

public ActionResult Index()
{
var n1 = from t1 in db.LanMu2
select t1;
var list = n1.ToList();
ViewBag.LanMu2Name = list;
return View();
}
public ActionResult Login()
{
return View();
}
[HttpPost]
public ActionResult LoginPost(string name, string password)
{
return RedirectToAction("Index");
}
}

显示View部分:

@{
Layout = null;
ViewBag.Title = "WenZhangGuanLi";
}

<!DOCTYPE html>

<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>文章管理</title>
<link href="~/Content/easyui.css" rel="stylesheet" />
<link href="~/Content/icon.css" rel="stylesheet" />
@*<link href="~/Content/demo.css" rel="stylesheet" />*@
<script src="~/Scripts/jquery-1.8.2.min.js"></script>
<script src="~/Scripts/jquery.easyui.min.js"></script>
<script src="~/Scripts/easyui-lang-zh_CN.js"></script>
<script type="text/javascript">
function edit1() {
var row = $('#dg').datagrid('getSelected');
if (!row) {
$.messager.alert('提示', '未选中任何行。');
return;
}
window.location.href = "/WenZhang/Edit/" + row.Id;
}

function add1() {
window.location.href = "/WenZhang/Edit/0"
}
function delect1() {
var row = $('#dg').datagrid('getSelected');
if (!row) {
$.messager.alert('提示', '未选中任何行。');
return;
}
if (confirm("确定要删除?")) {
url = "/WenZhang/Delete";
parameter = { id: row.Id };
$.post(url, parameter, function (data) {
alert("删除成功!");
window.location = "/WenZhang/Index";
});
}

}
</script>

</head>

<body id="Qbody">

<div id="dlg-toolbar">
<a href="#" class="easyui-linkbutton" iconCls="icon-add" plain="true" onclick="add1()">添加</a>
<a href="#" class="easyui-linkbutton" iconCls="icon-save" plain="true" onclick="edit1()">编辑</a>
<a href="#" class="easyui-linkbutton" iconCls="icon-remove" plain="true" onclick="delect1()">删除</a>
</div>
<table id="dg" title="文章信息" class="easyui-datagrid" style="98%;height:390px"
url="/WenZhang/JsonPost?id=@ViewBag.temp" pagination="true" toolbar="#dlg-toolbar"//此处URL传递回                                               来    的是WenZhangController里的temp
rownumbers="true" fitColumns="true" singleSelect="true">

<thead>

<tr>
<th field="Id">Id</th>
<th field="Title" width="80">文章名称</th>
@*<th field="Content" width="50">内容</th>*@
<th field="Editor" width="20">添加人</th>
@*<th field="CreateTime" width="50">创建时间</th>*@
<th field="BrowseCount" width="20">点击量</th>
@*<th field="LanMu2_Id" width="80">所属栏目</th>*@
</tr>
</thead>
</table>
</body>
</html>

后台WenZhangController也有所改动:

public class WenZhangController : Controller
{
//
// GET: /WenZhang/

public ActionResult Index(int id)
{
ViewBag.temp= id;
return View();
}
public ActionResult JsonPost(int id)
{
DropletsEntities dl = new DropletsEntities();
var p = from t in dl.WenZhang
//join t2 in dl.LanMu2 on t.LanMu2_Id equals t2.Id 此处修改,不在全部显示
where t.LanMu2_Id==id

select new
{
Id=t.Id,
Title = t.Title, 
Editor = t.Editor,
CreateTime = t.CreateTime, 
BrowseCount = t.BrowseCount,
//LanMu2_Id = t2.Name, 
};
return Json(p, JsonRequestBehavior.AllowGet);
}
public ActionResult Edit(int id)
{
WenZhang wenzhang;
if (id > 0)
{
DropletsEntities dao = new DropletsEntities();
var q = from t in dao.WenZhang where t.Id == id select t;
wenzhang = q.FirstOrDefault();
}
else
wenzhang = new WenZhang();

DropletsEntities dao1 = new DropletsEntities();
var list = (from t in dao1.LanMu2 select new { Id = t.Id, Name = t.Name }).ToList();
if (id == 0)
{
//4个参数:1数据,2值字段,3显示字段,4选中值
ViewBag.LanMu2_Id = new SelectList(list, "Id", "Name", 1);
}
else
{
DropletsEntities newent = new DropletsEntities();
var q = from t in newent.LanMu2 where t.Id == wenzhang.LanMu2_Id select t;
LanMu2 e = q.FirstOrDefault();
ViewBag.LanMu2_Id = new SelectList(list, "Id", "Name", e.Id);
}
return View(wenzhang);
}
[HttpPost]
[ValidateInput(false)]
public ActionResult EditPost(WenZhang wenzhang)
{
DropletsEntities dao = new DropletsEntities();
if (wenzhang.Id == 0)
{
wenzhang.CreateTime = DateTime.Now;
dao.WenZhang.Add(wenzhang);
}
else
{
var temp = dao.WenZhang.FirstOrDefault(t => t.Id == wenzhang.Id);
temp.Title = wenzhang.Title;
temp.Editor = wenzhang.Editor;
temp.LanMu2_Id = wenzhang.LanMu2_Id;
temp.Content = wenzhang.Content;
temp.PictureCaptions = wenzhang.PictureCaptions;
}
dao.SaveChanges();
dao.Dispose();
return RedirectToAction("Edit", new { id = wenzhang.Id });
}
[HttpPost]
public ActionResult Delete(int id)
{
DropletsEntities dao = new DropletsEntities();
var q = dao.WenZhang.FirstOrDefault(t => t.Id ==id);
dao.WenZhang.Remove(q);
dao.SaveChanges();
return RedirectToAction("Index");
}


}
原文地址:https://www.cnblogs.com/DotaSteam/p/5435547.html