修改MVCfrom表单

[HttpGet]
public ActionResult xiugai(int id=0)
{

var checkedlist = new List<checklist>() {
new checklist(){ id =1, name ="打球"},
new checklist(){ id =2, name ="唱歌"},
new checklist (){ id=3, name="唱戏"},
new checklist(){ id =4, name="敲代码"}
};
ViewBag.che = checkedlist;

var m = new BLL.studentbll().getdatatable().Find(T => T.id == id);
var model = new studentM
{
id = id,
age = m.age,
aihao = m.aihao,
homeid = m.homeid,
img = m.img,
name = m.name,
sex = m.sex,
shijian = m.shijian
};
var result = new BLL.homebll().getdatatble();
SelectList list = new SelectList(result, "homeid", "homename");
ViewBag.list = list;
return View(model);
}
[HttpPost]
public ActionResult xiugai(studentM m)
{
var bql = Request.Form["aihao"];
//获取图片的字段
var file = Request.Files["newImg"];

if (file.FileName != "")
{
//获取图片的路径
var filepath = "/Content/img/" + file.FileName;
//转化为虚拟路径
file.SaveAs(Server.MapPath(filepath));
//转化三层的数据
var model = new studentmodel
{
id = m.id,
img = filepath,
shijian = m.shijian,
aihao = bql,
homeid = m.homeid,
age = m.age,
sex = m.sex,
name = m.name
};
//接受bll层的方法
var result = new BLL.studentbll().xiugai(model);
if (result > 0)
{
return Content("<script>alert('修改成功');location.href='/student/Index'</script>");
}
else
{
return Content("<script>alert('修改失败');</script>");
}
}
else
{
var model = new studentmodel
{
id = m.id,
img = m.img,
shijian = m.shijian,
aihao = bql,
homeid = m.homeid,
age = m.age,
sex = m.sex,
name = m.name
};
//接受bll层的方法
var result = new BLL.studentbll().xiugai(model);
if (result > 0)
{
return Content("<script>alert('修改成功');location.href='/student/Index'</script>");
}
else
{
return Content("<script>alert('修改失败');</script>");
}
}
}

posted @ 2017-11-22 19:28  Mr.htb  阅读(130)  评论(0编辑  收藏  举报