asp.net mvc 增删改查

数据表:ls_help_type    数据库模型:ZYXModel

Controller:

ZYXModel.DataEntities db = new ZYXModel.DataEntities();//数据上下文对象

******************************Index控制器:查询数据列表
/// <summary>
        /// 问题类型首页(查)
        /// </summary>
        /// <returns></returns>
public ActionResult Index()
        {
            List<ZYXModel.ls_help_type> list = (from d in db.ls_help_type select d).ToList();
           //使用ViewData将数据传给View
            ViewData["DataList"] = list;
            return View();
        }

******************************Index对应视图:
<div id="nav" class="tc"><a href="/Admin//Helps/Index" class="navn">问题类型管理</a><a href="/Admin/Helps/queIndex" class="nava">问题管理</a></div>
<div id="main">
    <table class="table" style="text-align:center   ">       
   <!--要取到数据就直接@对应的变量 这样就可以了(导入命名空间也是一个道理)-->
        <tr>
            <th>编号</th>
            <th>问题类型</th>
            <th>操作</th>
        </tr>
    @foreach (ZYXModel.ls_help_type a in ViewData["DataList"] as List<ZYXModel.ls_help_type>) 
    {
        <tr>
        <td>@a.id</td>
        <td>@a.helpDescribe</td>
        <td>
            <a href="/Admin/Helps/Modify/@a.id">修改</a>
            <a href="/Admin/Helps/Del/@a.id">删除</a>
        </td>
        </tr>
    }     
        <tr><td></td><td></td><td><a href="/Admin/Helps/AddHelType">增加</a></td></tr>
     </table>

</div>



******************************Get过来的AddHelType控制器:返回视图
/// <summary>
        /// 增加问题类型,get过来调用此控制器(增)
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public ActionResult AddHelType()
        {
            return View();
        }

******************************[HttpGet]AddHtlType对应视图:
@model ZYXModel.ls_help_type
@using (Html.BeginForm("AddHelType", "Helps", FormMethod.Post)) 
    {
        <table>
            <tr style="text-align:center">
                <td colspan="2">增加</td>
            </tr>
            <tr>
                <td>问题类型:</td>
                <td><input type="text" name="helType"</td>
                <td><input type="submit" value="确定增加" />@Html.ActionLink("返回","Index","Helps")</td>
            </tr>
        </table>
}
******************************Post过来的AddHelType控制器:增加数据
        /// <summary>
        /// 增加问题类型,post过来调用此控制器(增)
        /// </summary>
        /// <param name="borrowId"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult AddHelType(string helType)
        {
            string helpTypeNmae = helType  ;
            string sql = "insert into ls_help_type (helpDescribe) values('" + helpTypeNmae + "');";
            int lines = SqlHelper.ExecuteNonQuery(sql);//执行操作,返回受影响的行数
            if (lines >= 1)
            {//数据插入成功,返回前台json对象
                return RedirectToAction("Index", "Helps");
            }
            else
            {//数据插入失败,返回前台json对象
                return RedirectToAction("Index", "Error");
            }
        }

******************************[HttpPost]AddHtlType对应视图:
@model ZYXModel.ls_help_type
@using (Html.BeginForm("AddHelType", "Helps", FormMethod.Post)) 
    {
        <table>
            <tr style="text-align:center">
                <td colspan="2">增加</td>
            </tr>
            <tr>
                <td>问题类型:</td>
                <td><input type="text" name="helType"</td>
                <td><input type="submit" value="确定增加" />@Html.ActionLink("返回","Index","Helps")</td>
            </tr>
        </table>

    }



******************************get过来的Modify控制器:返回修改视图
/// <summary>
        /// 修改问题类型,get过来调用此控制器(改)
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        public ActionResult Modify(int id)
        {
            ZYXModel.ls_help_type art = (from d in db.ls_help_type where d.id == id select d).FirstOrDefault();
            //将数据传递给视图:用ViewBag viewData 使用view的构造函数
            return View(art);
        }

******************************[HttpGet]Modify对应视图:
@model ZYXModel.ls_help_type
@using (Html.BeginForm("Modify", "Helps", FormMethod.Post)) 
    {
        <table>
            <tr>
                <td colspan="2">修改</td>
            </tr>
            <tr>
                <td>问题类型:@Html.HiddenFor(a=>a.id)</td>
                @*<td>@Html.TextBox("textName",(object)Model.CustomerNo)</td>*@
                <!--使用htmlHelper的强类型方法,直接从Model中根据id生成文本框-->
                <td>@Html.TextBoxFor(a =>a.helpDescribe)</td>
                <td><input type="submit" value="确定修改" />@Html.ActionLink("返回","Index","Helps")</td>
            </tr>
        </table>
}
******************************post过来的Modify控制器:修改对应数据
        /// <summary>
        /// 修改问题类型,post过来调用此控制器(改)
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost] 
        public ActionResult Modify(ZYXModel.ls_help_type model)
        {
            try
            {
                //1,将实体对象加入EF对象容器中,并获取伪包装类对象
                DbEntityEntry<ZYXModel.ls_help_type> entry = db.Entry<ZYXModel.ls_help_type>(model);
                //2,将伪包装类对象的状态设置为unchanged  
                entry.State = System.Data.Entity.EntityState.Unchanged;
                //3,设置被改变的属性
                entry.Property(a => a.helpDescribe).IsModified = true;
                //4,提交到数据库 完成修改
                db.SaveChanges();
                return RedirectToAction("Index", "Helps");
            }
            catch (Exception)
            {
                return RedirectToAction("Test", "Test");
            }
        }

******************************[HttpGet]Modify对应视图:
@model ZYXModel.ls_help_type
@using (Html.BeginForm("Modify", "Helps", FormMethod.Post)) 
    {
        <table>
            <tr>
                <td colspan="2">修改</td>
            </tr>
            <tr>
                <td>问题类型:@Html.HiddenFor(a=>a.id)</td>
                @*<td>@Html.TextBox("textName",(object)Model.CustomerNo)</td>*@
                <!--使用htmlHelper的强类型方法,直接从Model中根据id生成文本框-->
                <td>@Html.TextBoxFor(a =>a.helpDescribe)</td>
                <td><input type="submit" value="确定修改" />@Html.ActionLink("返回","Index","Helps")</td>
            </tr>
        </table>

}



******************************Del控制器:删除数据
        /// <summary>
        /// 删除问题类型(删)
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ActionResult Del(int id) //这个id是通过超链接带过来的
        {
            try
            {
                //需要一个实体对象参数
                //db.Customers.Remove(new Customer() {CustomerNo = id });
                //1,创建要删除的对象
                ZYXModel.ls_help_type modelDel = new ZYXModel.ls_help_type() { id = id };
                //2,将对象添加到EF管理容器中
                db.ls_help_type.Attach(modelDel);
                //3,修改对象的包装类对象标识为删除状态
                db.ls_help_type.Remove(modelDel);
                //4,更新到数据库
                db.SaveChanges();
                //5,更新成功,则命令流浪器重定向 到 /Home/Index 方法
                return RedirectToAction("Index", "Helps");
            }
            catch (Exception)
            {
                //指定对应跳转的视图Test下的Test.cshtml文件
                return RedirectToAction("Index", "Error");
                //return Content("删除失败" + ex.Message);
            }
        }
  • 2
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值