首页 > 其他 > 详细

导入,下载,增删查改

时间:2017-11-23 10:20:40      阅读:288      评论:0      收藏:0      [点我收藏+]

***********************************************************************************************************

Api:

public class StuController : ApiController
{
//实例化对象
Day3Entities ent = new Day3Entities();

//显示

// GET api/stu
public List<Student> Get()
{
return ent.Students.ToList();
}

//查询

//GET api/stu/5
public Student Get(string name)
{
var list = ent.Students.Where(T=>T.NAME.Contains(name)).FirstOrDefault();
return list;
}
//public Student Get(int id)
//{
// var list = ent.Students.Where(T => T.ID==id).FirstOrDefault();
// return list;


//}

//添加

// POST api/stu
public void Post([FromBody]Student stu)
{
var res = ent.Students.Add(stu);
ent.SaveChanges();
}

//修改

// PUT api/stu/5
public void Put([FromBody]Student stu)
{
var res = ent.Students.Where(T => T.ID == stu.ID).FirstOrDefault();
res.NAME = stu.NAME;
res.Sex = stu.Sex;
res.Age = stu.Age;

ent.SaveChanges();
}

//删除

// DELETE api/stu/5
public void Delete(int id)
{
var res = ent.Students.Where(T => T.ID == id).FirstOrDefault();
ent.Students.Remove(res);
ent.SaveChanges();

}
}

***********************************************************************************************************

//显示数据

public class StuMvcController : Controller
{
//
// GET: /StuMvc/

//定义一个基地址
public static readonly Uri address = new Uri("http://localhost:61136/");
public ActionResult Index(int pageIndex=1)
{
//实例化泛型
List<Student> list = new List<Student>();

//获取api的方法
Uri url = new Uri(address, "/api/stu");
//引用命名空间
using (HttpClient client = new HttpClient())
{
var result = client.GetAsync(url).Result;
//判断返回值状态
if (result.IsSuccessStatusCode)
{
list = result.Content.ReadAsAsync<List<Student>>().Result;
}
}
return View(list.ToPagedList(pageIndex,3));
}

/// <summary>
/// 添加显示页面
/// </summary>
/// <returns></returns>
public ActionResult Add()
{
//自定义下拉列表
List<SelectListItem> liAge = new List<SelectListItem>() {
new SelectListItem(){Value="8",Text="8"},
new SelectListItem(){Value="18",Text="18"},
new SelectListItem(){Value="20",Text="20"},
new SelectListItem(){Value="21",Text="21"}
};

///SelectList list = new SelectList(dt, "Lid", "LName");

ViewBag.liAge = liAge;
return View();
}

/// <summary>
/// 添加
/// </summary>
/// <param name="stu"></param>
/// <returns></returns>
[HttpPost]
public ActionResult Add(Student stu)
{
//获取api的方法
Uri url = new Uri(address, "/api/Stu");
//引用命名空间
using (HttpClient client = new HttpClient())
{
var result = client.PostAsJsonAsync(url, stu).Result;
if (result.IsSuccessStatusCode)
{
return Content("<script>alert(‘添加成功‘);location.href=‘/StuMvc/Index‘;</script>");
}
else
{
return Content("<script>alert(‘添加失败‘)</script>");
}
}
}
/// <summary>
/// 删除
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
public ActionResult Delete(int id)
{
//获取api的方法
Uri url = new Uri(address, "/api/Stu/"+id);
//引用命名空间
using (HttpClient client = new HttpClient())
{
var result = client.DeleteAsync(url).Result;
//判断返回值
if (result.IsSuccessStatusCode)
{
return Content("<script>alert(‘删除成功‘);location.href=‘/StuMvc/Index‘;</script>");
}
else
{
return Content("<script>alert(‘删除失败‘)</script>");
}
}
}
/// <summary>
/// 查询单个值
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
//public ActionResult Upd(string id)
//{
// Student list = new Student();
// Uri url = new Uri(address, "/api/stu/" + id);
// //引用命名空间
// using (HttpClient client = new HttpClient())
// {
// var result = client.GetAsync(url).Result;
// //判断返回值状态
// if (result.IsSuccessStatusCode)
// {
// list = result.Content.ReadAsAsync<Student>().Result;
// }

// return Json(list, JsonRequestBehavior.AllowGet);
// }
//}

/// <summary>
/// 修改显示页面
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
public ActionResult Upd(int id)
{
//自定义下拉列表
List<SelectListItem> liAge = new List<SelectListItem>() {
new SelectListItem(){Value="8",Text="8"},
new SelectListItem(){Value="18",Text="18"},
new SelectListItem(){Value="20",Text="20"},
new SelectListItem(){Value="21",Text="21"}
};
ViewBag.liAge = liAge;
//建立接口
Day3Entities db = new Day3Entities();
//取出要修改的数据
var data = db.Students.Where(t => t.ID == id).FirstOrDefault();
//创建学生类接收数据
var model = new Student()
{
Age = data.Age,
ID = id,
NAME = data.NAME,
Sex=data.Sex
};
return View(model);
}

/// <summary>
/// 修改
/// </summary>
/// <param name="stu"></param>
/// <returns></returns>
[HttpPost]
public ActionResult Upd(Student stu)
{
//获取api的方法
Uri url = new Uri(address, "/api/Stu");
//引用命名空间
using (HttpClient client = new HttpClient())
{
var res=client.PutAsJsonAsync(url,stu).Result;
if (res.IsSuccessStatusCode)
{
return Content("<script>alert(‘修改成功‘);location.href=‘/StuMvc/Index‘;</script>");
}
else
{
return Content("<script>alert(‘修改失败‘)</script>");
}
}
}

}

***********************************************************************************************************

//添加页面
<div>
@using( Html.BeginForm())
{
<p>学生姓名:@Html.TextBoxFor(T => T.NAME)</p>
<p>学生性别:@Html.RadioButtonFor(T=>T.Sex,"男")男
@Html.RadioButtonFor(T => T.Sex, "女")女
</p>
<p>学生年龄:@Html.DropDownListFor(T => T.Age, ViewBag.liAge as List<SelectListItem>)</p>

//////////<p>@Html.DropDownListFor(T=>T.Lid,ViewBag.li as SelectList)</p>


<p><input id="Submit1" type="submit" value="提交" /></p>
}

</div>


**************************************************************************************************************

//压缩

public ActionResult Index()
{
ZipHandler.ZipDirectory(@"D:\Private\Demo\", @"D:\Private\Demo1.zip", "123");
ZipHandler.UnZipFile(@"D:\Private\Demo1.zip", @"D:\Private\Demo1", "123");
return View();
}

**************************************************************************************************************

/// <summary>
/// 上传(添加后台)
/// </summary>
/// <returns></returns>
///
[HttpPost]
public ActionResult shangchaun(string Name, string Infotype, HttpPostedFileBase ExcFile)
{
if (ExcFile == null)
{
return View();
}

string strfileName = Server.MapPath("/EXCEL/"); //上传的路径 strfileName路径名
if (!Directory.Exists(strfileName))
{
Directory.CreateDirectory(strfileName);
}
string fName = Path.GetFileName(ExcFile.FileName); //获取上传时的文件名
string fNewName = string.Format("{0}.{1}",Guid.NewGuid().ToString(),fName.Split(‘.‘)[1]);//获取一个不重复的文件名 保存到数据库
ExcFile.SaveAs(strfileName+ fNewName);//保存到本地

InfoM m = new InfoM();
m.Name = Name;
m.Infotype = Infotype;
m.SCName = fName;
m.SCNewName = fNewName;

var result = bll.addinfo(m);
if (result > 0)
{
return Content("<script>alert(‘上传成功‘);location.href=‘showinfo‘</script>");
}
else
{
return Content("<script>alert(‘上传失败‘)</script>");
}

}

**************************************************************************************************************
/// <summary>
/// 导出
/// </summary>
/// <returns></returns>
public FileResult xiazai()
{
var result = bll.showinfo();
if (result != null || result.Count > 0)
{
HSSFWorkbook book = new HSSFWorkbook();
ISheet sheet = book.CreateSheet("sheet1");
IRow row = sheet.CreateRow(0);
row.CreateCell(0).SetCellValue("编号");
row.CreateCell(1).SetCellValue("名称");
row.CreateCell(2).SetCellValue("类型");
row.CreateCell(3).SetCellValue("时间");

for (int i = 0; i < result.Count; i++)
{
IRow rows = sheet.CreateRow(i + 1);
rows.CreateCell(0).SetCellValue(result[i].ID);
rows.CreateCell(1).SetCellValue(result[i].Name);
rows.CreateCell(2).SetCellValue(result[i].Infotype);
rows.CreateCell(3).SetCellValue(result[i].Infotime);
}
MemoryStream rs = new MemoryStream();
book.Write(rs);
rs.Seek(0, SeekOrigin.Begin);
return File(rs, "application/ved-excel", "学生信息.xls");
}
else
{
MemoryStream rs = new MemoryStream();
rs.Seek(0, SeekOrigin.Begin);
return File(rs, "application/ved-excel", "学生信息.xls");
}

}

*****************************************************************************************************************

//上传页面

<form action="@Url.Action("shangchaun")" method="post" enctype="multipart/form-data">
名字:<input id="Text1" type="text" name="Name" /><br />
类型:<input id="Text2" type="text" name="Infotype" /><br />
<input id="File1" type="file" name="ExcFile" />
<input id="Submit1" type="submit" value="上传" />
</form>

导入,下载,增删查改

原文:http://www.cnblogs.com/dxylx/p/7883280.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!