MVC 基本操作
2014-09-15 15:07 愿得一人心 阅读(128) 评论(0) 编辑 收藏 举报#region 首页 public ActionResult Index() { string User_Test_Select = "User_Test_Select"; var item = DBhelp.GetList<test_model>(User_Test_Select); return View(item); } #endregion #region 增加 public ActionResult Create() { return View(); } #region Post 传值 [HttpPost] public ActionResult Create(Models.test_model model) { string User_Test_Add = "insert into Test (name,age) values('" + model.name + "'," + model.age + ")"; int i = DBhelp.ExecuteSql(User_Test_Add); if (i > 0) { return RedirectToAction("Index"); } else { return View(); } } #endregion #endregion #region 修改 public ActionResult Edit(int id) { SqlParameter[] parameters = { new SqlParameter("@ID", SqlDbType.Int,4) }; parameters[0].Value = id; DataSet ds = DBhelp.RunProcedure("User_Test_Select_One", parameters, "test"); test_model model = new test_model(); if (ds.Tables[0].Rows.Count > 0) { if (ds.Tables[0].Rows[0]["ID"] != null && ds.Tables[0].Rows[0]["ID"].ToString() != "") { model.ID = int.Parse(ds.Tables[0].Rows[0]["ID"].ToString()); } if (ds.Tables[0].Rows[0]["name"] != null && ds.Tables[0].Rows[0]["name"].ToString() != "") { model.name = ds.Tables[0].Rows[0]["name"].ToString(); } if (ds.Tables[0].Rows[0]["age"] != null && ds.Tables[0].Rows[0]["age"].ToString() != "") { model.age = int.Parse(ds.Tables[0].Rows[0]["age"].ToString()); } } return View(model); } #region post传值 [HttpPost] public ActionResult Edit(Models.test_model model) { string User_Update = "Update Test set name='" + model.name + "' , age=" + model.age + " where id=" + model.ID; int i = DBhelp.ExecuteSql(User_Update); if (i > 0) { return RedirectToAction("Index"); } else { return View(); } } #endregion #endregion #region get 传值删除 [HttpGet] public ActionResult Delete(int id) { string User_Test_Delect = "delete Test where ID= " + id; int i = DBhelp.ExecuteSql(User_Test_Delect); string url = Request.UrlReferrer == null ? "UserTest/Index" : Request.UrlReferrer.ToString(); if (i > 0) { return Redirect(url); } else { return Redirect(url); } } #endregion