MVC3.0 修改操作

来源:互联网 发布:js禁用滚动条事件 编辑:程序博客网 时间:2024/04/30 13:01

  #region 0.4 显示要修改的数据 +ActionResult Modify(int id)
        [HttpGet]   

public ActionResult Modify(int id)
        {
            //1.根据id 查询数据库,返回的集合中 拿到 第一个 实体对象
            BlogArticle art = (from a in db.BlogArticles where a.AId == idselect a).FirstOrDefault();
            //2.生成 文章分类 下拉框 列表集合  <option value="1">文本</option>
            IEnumerable<SelectListItem> listItem = (from c in db.BlogArticleCates
                                          where c.IsDel == false select c).ToList() //生成得一个集合

                                          .Select(c=> new SelectListItem { Value = c.Id.ToString(), Text = c.Name });//遍历集合生成SelectListItem 集合并返回给上面的集合
            //将生成的文章分类 下拉框选项集合 设置给 ViewBag
            ViewBag.CateList = listItem;
            
            //List<SelectListItem> list;


            //3.将 art 传递 给 视图显示
            //ViewBag
            //ViewData
            //* “加载”视图,使用View的构造函数,将 数据 传给 视图上的 名为 Model 的 属性
            return View(art);
        } 

   #region 0.5 执行修改 +ActionResult Modify(BlogArticle model)
        [HttpPost]
        /// <summary>
        /// 0.5 执行修改
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public ActionResult Modify(BlogArticle model)
        {
            try
            {
                //1.将实体对象 a.加入 EF 对象容器中,并 b.获取 伪包装类对象
                DbEntityEntry<BlogArticle> entry = db.Entry<BlogArticle>(model);
                //2.将包装类对象的状态设置为 unchanged
                entry.State = System.Data.EntityState.Unchanged;
                //3.设置 被改变的属性
                entry.Property(a => a.ATitle).IsModified = true;
                entry.Property(a => a.AContent).IsModified = true;
                entry.Property(a => a.ACate).IsModified = true;


                //4.提交到数据库 完成修改
                db.SaveChanges();
                //5.更新成功,则命令浏览器 重定向 到 /Home/List 方法
                return RedirectToAction("Index", "Home");
            }
            catch (Exception ex)
            {
                return Content("修改失败~~~" + ex.Message);
            }
        } 
        #endregion

知识点生成表单。

@model MVCBlog.Models.BlogArticle
<!--指定页面Model 属性 的类型-->
@{
    Layout = null;
}

 @using (Html.BeginForm("Modify", "Home", FormMethod.Post))
 


        <table id="tbList">
            <tr>
                <td colspan="2">修改 @Html.HiddenFor(a=>a.AId) </td>
            </tr>
            <tr>
                <td>标题:</td>
                @*<td>@Html.TextBox("txtName",(object)Model.ATitle)</td>*@


                <!--使用HtmlHelper的强类型方法 直接 从 Model 中 根据 ATitle 属性生成文本框-->
                <td>@Html.TextBoxFor(a=>a.ATitle)</td>
            </tr>
            <tr>
                <td>分类:</td>
                <!--使用强类型方法生成下拉框,并自动根据 model属性里的ACate值 设置 下拉框的默认选中项-->
                <td>@Html.DropDownListFor(a=>a.ACate,ViewBag.CateList as IEnumerable<SelectListItem>)</td>
            </tr>
            <tr>
                <td>内容:</td>
                <!--使用HtmlHelper的强类型方法 直接 从 Model 中 根据 AContent 属性生成文本域-->
                <td>@Html.TextAreaFor(a => a.AContent, 10, 60, null)</td>
            </tr>
            <tr>
                <td colspan="2"><input type="submit" value="确定修改" /> @Html.ActionLink("返回","Index","Home")</td>
            </tr>
        </table>
    }