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 = 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

  

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。