首页 > 代码库 > MVC 基本操作
MVC 基本操作
#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 = http://www.mamicode.com/id;"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
MVC 基本操作
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。