博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
学用MVC4做网站三:3.3更新栏目
阅读量:4539 次
发布时间:2019-06-08

本文共 6331 字,大约阅读时间需要 21 分钟。

3.3更新栏目

上次在树形列表里面点击栏目名称后跳转到详细信息页面~/Category/ManageDetails/id。在详细页面里点修改,来完成栏目资料修改。

先打开【CategoryController】添加[ManageDetails(int id)]action

///         /// 栏目详细资料        ///         ///         /// 
public ActionResult ManageDetails(int id) { categoryRsy = new CategoryRepository(); var _node = categoryRsy.Find(id); if (_node == null) { Error _e = new Error { Title = "栏目不存在", Details = "栏目不存在", Cause = Server.UrlEncode("
  • 栏目已经删除
  • "), Solution = Server.UrlEncode("
  • 返回栏目栏目管理
  • ") }; return RedirectToAction("ManageError", "Prompt", _e); } ModuleRepository _moduleRsy = new ModuleRepository(); var _modules = _moduleRsy.List(true); List
    _slimodule = new List
    (_modules.Count()); foreach (Module _module in _modules) { if (_node.Model == _module.Model) _slimodule.Add(new SelectListItem { Text = _module.Name, Value = _module.Model, Selected = true }); else _slimodule.Add(new SelectListItem { Text = _module.Name, Value = _module.Model }); } ViewData.Add("Model", _slimodule); var _type = TypeSelectList; _type.SingleOrDefault(t => t.Value == _node.Type.ToString()).Selected = true; ViewData.Add("Type", _type); return View(_node); }

    代码先是看栏目是否存在,不存在跳转到错误页面,后面是添加"Model“和“Type”的ViewData

    右键添加强类型视图ManageDetails.cshtml,内容基本与ManageAdd.cshtml类似

    @model Ninesky.Models.Category@{    ViewBag.Title = "栏目信息";    Layout = "~/Views/Layout/_Manage.cshtml";}
    栏目信息
    @using (Html.BeginForm("ManageUpdate","Category")) { @Html.ValidationSummary(true)
    详细资料
      @Html.HiddenFor(model => model.CategoryId)
    • @Html.LabelFor(model => model.CategoryId)
      @Html.DisplayTextFor(model => model.CategoryId)
    • @Html.LabelFor(model => model.Name)
      @Html.EditorFor(model => model.Name) @Html.ValidationMessageFor(model => model.Name)
    • @Html.LabelFor(model => model.ParentId)
      @Html.TextBoxFor(model => model.ParentId, new { @class = "easyui-combotree", data_options = "url:'" + Url.Action("JsonTreeParent", "Category") + "'" }) @Html.ValidationMessageFor(model => model.ParentId)
    • @Html.LabelFor(model => model.Type)
      @Html.DropDownList("Type") @Html.ValidationMessageFor(model => model.Type)
    • @Html.LabelFor(model => model.Model)
      @Html.DropDownList("Model") @Html.ValidationMessageFor(model => model.Model)
    • @Html.LabelFor(model => model.CategoryView)
      @Html.EditorFor(model => model.CategoryView) @Html.ValidationMessageFor(model => model.CategoryView)
    • @Html.LabelFor(model => model.ContentView)
      @Html.EditorFor(model => model.ContentView) @Html.ValidationMessageFor(model => model.ContentView)
    • @Html.LabelFor(model => model.Navigation)
      @Html.EditorFor(model => model.Navigation) @Html.ValidationMessageFor(model => model.Navigation)
    • @Html.LabelFor(model => model.Order)
      @Html.EditorFor(model => model.Order) @Html.ValidationMessageFor(model => model.Order)
    }
    @Html.Action("ManagePartialTree", "Category")
    @section Scripts { @Styles.Render("~/EasyUi/icon") @Scripts.Render("~/bundles/EasyUi") @Scripts.Render("~/bundles/jqueryval")}

    注意的是 @using (Html.BeginForm("ManageUpdate","Category"))这句;表示点修改按钮的后是向ManageUpdate提交数据。下面开始做这个action

    在【CategoryController】里添加httppost方式的[ManageUpdate]action

    ///         /// 修改栏目信息        ///         ///         /// 
    public ActionResult ManageUpdate(Category category) { switch (category.Type) { case 0: category.Navigation = ""; break; case 1: category.Model = ""; category.ContentView = ""; category.Navigation = ""; break; case 2: category.Model = ""; category.CategoryView = ""; category.ContentView = ""; break; } categoryRsy = new CategoryRepository(); if (categoryRsy.Update(category)) { Notice _n = new Notice { Title = "修改栏目成功", Details = "修改栏目成功!", DwellTime = 5, NavigationName = "栏目详细信息", NavigationUrl = Url.Action("ManageDetails", "Category", new { id = category.CategoryId }) }; return RedirectToAction("ManageNotice", "Prompt", _n); } else { Error _e = new Error { Title = "修改栏目失败", Details = "在修改栏目信息时,未能保存到数据库", Cause = "系统错误", Solution = Server.UrlEncode("
  • 返回栏目详细资料页面,修改信息后重新操作
  • 联系网站管理员
  • ") }; return RedirectToAction("ManageError", "Prompt", _e); } }

    很简单,首先是判断栏目类型,根据栏目类型清除无关数据,然后将修改保存到数据库。

    试一下将“测试栏目”改成“公司简介”

    保存成功!

    =================================

    代码包:

    转载于:https://www.cnblogs.com/mzwhj/archive/2012/11/29/2794465.html

    你可能感兴趣的文章
    如何查看方法在哪里被调用
    查看>>
    HUE的自动化安装部署
    查看>>
    图片服务器(FastDFS)的搭建
    查看>>
    myBatis应用
    查看>>
    RuntimeError: DataLoader worker (pid 18255) is killed by signal: Killed.
    查看>>
    [PHP] 用AppServ一步到位安装PHP服务器
    查看>>
    mac brew install redis 报错
    查看>>
    Work? working!
    查看>>
    开源收藏
    查看>>
    scipy插值interpolation
    查看>>
    C# BackgroundWorker
    查看>>
    移动对meta的定义
    查看>>
    (转载)char与byte的区别
    查看>>
    《零基础学习Python》01
    查看>>
    启动页面
    查看>>
    1005 继续(3n+1)猜想 (25 分)
    查看>>
    【洛谷P1641】[SCOI2010]生成字符串
    查看>>
    2014.8.15模拟赛【公主的朋友】
    查看>>
    Linux常用系统信息查看命令
    查看>>
    系统全局环境变量设置
    查看>>