UserInfoController.cs

本文介绍了一个基于ASP.NET MVC的应用程序中的用户管理功能实现,包括用户列表展示、用户注册、用户详情显示、用户数据的删除及修改等操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MvcUserDemo.Models;

namespace MvcUserDemo.Controllers
{
    public class UserInfoController : Controller
    {
        #region 用户列表
        public ActionResult Index()
        {
            //获取数据库中的UserInfo表中数据
            DataTable dt = SqlHelper.ExecuteDataTable("select Id, UserName, Age from  UserInfo");

            //把数据传递到前台页面进行展示
            ViewData["dt"] = dt;

            return View();
        }  
        #endregion

        #region 用户注册

        public ActionResult Add()
        {
            return View();
        }

        public ActionResult ProcessAdd(FormCollection collection)
        {
            //拿到表单里面传递来的数据
            string userName = Request["UserName"];
            int Age = int.Parse(collection["Age"]??"0");
            //往数据库插入数据
            string insertSql = "insert UserInfo values(@UserName,@Age)";
            SqlHelper.ExecuteNonQuery(insertSql,
                new SqlParameter("@UserName", userName),
                new SqlParameter("@Age", Age));
            
            //return Content("ok");
            //页面跳转到当前控制器的Index
            return RedirectToAction("Index");

        }
        #endregion


        #region 显示用户详情

        public ActionResult Show()
        {
            UserInfo userInfo =new UserInfo();
            userInfo.Id = 9;
            userInfo.UserName = "传智播客";
            userInfo.Age = 18;

            //ViewData["UserInfo"] = userInfo;

            ViewData.Model = userInfo;

            return View("Show2");
        }
        #endregion

        #region 删除
        //Action在执行之前,MVC框架会自动的将请求中的数据装配到Action的参数里面去。
        public ActionResult Delete(int UId)
        {
            //Request.QueryString["UId"]

            //跟进Id删除用户的数据
            string sql = "delete from UserInfo where Id=@Id";

            SqlHelper.ExecuteNonQuery(sql, new SqlParameter("@Id", UId));
            //页面跳转到删除后的首页

            return RedirectToAction("Index");
        }
        #endregion

        #region 修改
        //  /UserInfo/Edit/3
        //只是显示用户修改的页面
        [HttpGet]
        public ActionResult Edit(int Id)
        {
            string sql = "select UserName,Id,Age from UserInfo where Id=@Id";
            DataTable dt = SqlHelper.ExecuteDataTable(sql, new SqlParameter("@Id", Id));
            
            //把dt转成UserInfo对象
            UserInfo userInfo =new UserInfo();
            userInfo.Id = Convert.ToInt32(dt.Rows[0]["Id"]);
            userInfo.Age = Convert.ToInt32(dt.Rows[0]["Age"]);
            userInfo.UserName = dt.Rows[0]["UserName"].ToString();

            ViewData.Model = userInfo;

            return View();
        }


        //处理用户修改数据表单提交
        [HttpPost]
        public ActionResult Edit(int Id,int Age,string UserName,UserInfo userInfo)
        {
            string updateSql = "update UserInfo set UserName=@UserName,Age=@Age where Id=@Id";
            SqlParameter idParameter =new SqlParameter("@Id",userInfo.Id);
            SqlParameter ageParameter = new SqlParameter("@Age", userInfo.Age);
            SqlParameter nameParameter = new SqlParameter("@UserName", userInfo.UserName);

            SqlHelper.ExecuteNonQuery(updateSql, idParameter, ageParameter, nameParameter);

            return RedirectToAction("Index");
        }
        #endregion
    }

}








using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MvcUserDemo.Models;

namespace MvcUserDemo.Controllers
{
    public class UserInfoController : Controller
    {
        #region 用户列表
        public ActionResult Index()
        {
            //获取数据库中的UserInfo表中数据
            DataTable dt = SqlHelper.ExecuteDataTable("select Id, UserName, Age from  UserInfo");

            //把数据传递到前台页面进行展示
            ViewData["dt"] = dt;

            return View();
        }  
        #endregion

        #region 用户注册

        public ActionResult Add()
        {
            return View();
        }

        public ActionResult ProcessAdd(FormCollection collection)
        {
            //拿到表单里面传递来的数据
            string userName = Request["UserName"];
            int Age = int.Parse(collection["Age"]??"0");
            //往数据库插入数据
            string insertSql = "insert UserInfo values(@UserName,@Age)";
            SqlHelper.ExecuteNonQuery(insertSql,
                new SqlParameter("@UserName", userName),
                new SqlParameter("@Age", Age));
            
            //return Content("ok");
            //页面跳转到当前控制器的Index
            return RedirectToAction("Index");

        }
        #endregion


        #region 显示用户详情

        public ActionResult Show()
        {
            UserInfo userInfo =new UserInfo();
            userInfo.Id = 9;
            userInfo.UserName = "传智播客";
            userInfo.Age = 18;

            //ViewData["UserInfo"] = userInfo;

            ViewData.Model = userInfo;

            return View("Show2");
        }
        #endregion

        #region 删除
        //Action在执行之前,MVC框架会自动的将请求中的数据装配到Action的参数里面去。
        public ActionResult Delete(int UId)
        {
            //Request.QueryString["UId"]

            //跟进Id删除用户的数据
            string sql = "delete from UserInfo where Id=@Id";

            SqlHelper.ExecuteNonQuery(sql, new SqlParameter("@Id", UId));
            //页面跳转到删除后的首页

            return RedirectToAction("Index");
        }
        #endregion

        #region 修改
        //  /UserInfo/Edit/3
        //只是显示用户修改的页面
        [HttpGet]
        public ActionResult Edit(int Id)
        {
            string sql = "select UserName,Id,Age from UserInfo where Id=@Id";
            DataTable dt = SqlHelper.ExecuteDataTable(sql, new SqlParameter("@Id", Id));
            
            //把dt转成UserInfo对象
            UserInfo userInfo =new UserInfo();
            userInfo.Id = Convert.ToInt32(dt.Rows[0]["Id"]);
            userInfo.Age = Convert.ToInt32(dt.Rows[0]["Age"]);
            userInfo.UserName = dt.Rows[0]["UserName"].ToString();

            ViewData.Model = userInfo;

            return View();
        }


        //处理用户修改数据表单提交
        [HttpPost]
        public ActionResult Edit(int Id,int Age,string UserName,UserInfo userInfo)
        {
            string updateSql = "update UserInfo set UserName=@UserName,Age=@Age where Id=@Id";
            SqlParameter idParameter =new SqlParameter("@Id",userInfo.Id);
            SqlParameter ageParameter = new SqlParameter("@Age", userInfo.Age);
            SqlParameter nameParameter = new SqlParameter("@UserName", userInfo.UserName);

            SqlHelper.ExecuteNonQuery(updateSql, idParameter, ageParameter, nameParameter);

            return RedirectToAction("Index");
        }
        #endregion
    }
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值