控制器

using MODEL;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using BLL;
using Newtonsoft.Json;
using Webdiyer.WebControls.Mvc;

namespace ZuiHou.Controllers
{
public class IndexController : Controller
{
bll abll = new bll();
// GET: Index
public List<biao> GetStudentList()
{
DataTable dt = abll.GetStudentList();
List<biao> list = JsonConvert.DeserializeObject<List<biao>>(JsonConvert.SerializeObject(dt));
return list;
}
public ActionResult Index(string bid = "", string bname = "", string age = "", string lid = "", int index = 1)
{
UserSchool();
return View(Show(bid, bname, age, lid, index));
}
/// <summary>
/// 查询分页
/// </summary>
/// <param name="bid"></param>
/// <param name="bname"></param>
/// <param name="age"></param>
/// <param name="lid"></param>
/// <param name="index"></param>
/// <returns></returns>
public List<biao> Show(string bid = "", string bname = "", string age = "", string lid = "", int index = 1)
{
DataTable td = abll.ShowStudent(bid, bname, age, lid);
var linq = from s in td.AsEnumerable()
select new biao
{
bid = s.Field<int>("bid"),
bname = s.Field<string>("bname"),
age = s.Field<string>("age"),
lid = s.Field<int>("lid"),
};
return linq.ToPagedList(index, 2);
}
/// <summary>
/// 下拉框
/// </summary>
public void UserSchool()
{
DataTable td = abll.GetStudentList();
var linq = from s in td.AsEnumerable()
select new SelectListItem
{
Value = s.Field<int>("lid").ToString(),
Text = s.Field<string>("lname")
};
ViewBag.s = linq.ToList();
}
/// <summary>
/// 添加
/// </summary>
/// <returns></returns>
public ActionResult Create()
{
//下拉框的方法
UserSchool();
return View();
}
[HttpPost]
public ActionResult Create(biao s)
{
biao m = JsonConvert.DeserializeObject<biao>(JsonConvert.SerializeObject(s));
int i = abll.AddStudent(m);
if (i > 0)
{
Response.Write("<script>alert('添加成功')</script>");
}
return View();
}
/// <summary>
/// 删除
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
public ActionResult del(int id)
{
int i = abll.del(id);
if (i > 0)
{
Response.Write("删除成功");
return RedirectToAction("Index");//删除成功后刷新显示页面
}
else
{
Response.Write("删除失败");
return RedirectToAction("Index");
}
}
/// <summary>
/// 修改
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
public ActionResult upt(int id)
{
UserSchool();
biao s = GetStudentList().Find(m => m.bid == id);
return View(s);
}

[HttpPost]
public int upt(biao s)
{
biao m = JsonConvert.DeserializeObject<biao>(JsonConvert.SerializeObject(s));
return abll.upt(m);
}
}
}

posted @ 2018-07-30 01:50  真是善良  阅读(110)  评论(0编辑  收藏  举报