欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

MVC4制作网站教程第四章 更新栏目4.3

程序员文章站 2023-11-05 22:01:10
序  一、用户  二、用户组  三、栏目 3.1添加栏目 3.2浏览栏目  3.3更新栏目  上次在树形...


 一、用户
 二、用户组
 三、栏目
3.1添加栏目
3.2浏览栏目 
3.3更新栏目 

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

 先打开【categorycontroller】添加[managedetails(int id)]action 

/// <summary>
    /// 栏目详细资料
    /// </summary>
    /// <param name="id"></param>
    /// <returns></returns>
    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("<li>栏目已经删除</li>"), solution = server.urlencode("<li>返回<a href='" + url.action("manage", "cayegory") + "'>栏目栏目管理</a></li>") };
        return redirecttoaction("manageerror", "prompt", _e);
      }
      modulerepository _modulersy = new modulerepository();
      var _modules = _modulersy.list(true);
      list<selectlistitem> _slimodule = new list<selectlistitem>(_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";
}

<div class="workspace">
  <div class="inside">
    <div class="notebar">
      <img alt="" src="~/skins/default/manage/images/category.gif" />栏目信息
    </div>

    @using (html.beginform("manageupdate","category"))
    {
      @html.validationsummary(true)

      <fieldset>
        <legend>详细资料</legend>
        <ul>
          @html.hiddenfor(model => model.categoryid)
          <li>
            <div class="editor-label">
              @html.labelfor(model => model.categoryid)
            </div>
            <div class="editor-field">
              @html.displaytextfor(model => model.categoryid)
            </div>
          </li>
          <li>
            <div class="editor-label">
              @html.labelfor(model => model.name)
            </div>
            <div class="editor-field">
              @html.editorfor(model => model.name)
              @html.validationmessagefor(model => model.name)
            </div>
          </li>
          <li>
            <div class="editor-label">
              @html.labelfor(model => model.parentid)
            </div>
            <div class="editor-field">
              @html.textboxfor(model => model.parentid, new { @class = "easyui-combotree", data_options = "url:'" + url.action("jsontreeparent", "category") + "'" })
              @html.validationmessagefor(model => model.parentid)
            </div>
          </li>
          <li>
            <div class="editor-label">
              @html.labelfor(model => model.type)
            </div>
            <div class="editor-field">
              @html.dropdownlist("type")
              @html.validationmessagefor(model => model.type)
            </div>
          </li>
          <li id="li_model">
            <div class="editor-label">
              @html.labelfor(model => model.model)
            </div>
            <div class="editor-field">
              @html.dropdownlist("model")
              @html.validationmessagefor(model => model.model)
            </div>
          </li>
          <li id="li_categoryview">
            <div class="editor-label">
              @html.labelfor(model => model.categoryview)
            </div>
            <div class="editor-field">
              @html.editorfor(model => model.categoryview)
              @html.validationmessagefor(model => model.categoryview)
            </div>
          </li>
          <li id="li_contentview">
            <div class="editor-label">
              @html.labelfor(model => model.contentview)
            </div>
            <div class="editor-field">
              @html.editorfor(model => model.contentview)
              @html.validationmessagefor(model => model.contentview)
            </div>
          </li>
          <li id="li_nav">
            <div class="editor-label">
              @html.labelfor(model => model.navigation)
            </div>
            <div class="editor-field">
              @html.editorfor(model => model.navigation)
              @html.validationmessagefor(model => model.navigation)
            </div>
          </li>
          <li>
            <div class="editor-label">
              @html.labelfor(model => model.order)
            </div>
            <div class="editor-field">
              @html.editorfor(model => model.order)
              @html.validationmessagefor(model => model.order)
            </div>
          </li>
          <li>
            <div class="editor-label">
              <input id="submit1" type="submit" value="修改" />
            </div>
            <div class="editor-field">
              
            </div>
            
          </li>
        </ul>
      </fieldset>
    }
  </div>
</div>
<div class="left">
  <div class="top"></div>
  @html.action("managepartialtree", "category")
</div>
<div class="split"></div>
<div class="clear"></div>
<script type="text/javascript">
  details();
  $("#type").change(function () {
    details();
  });
  function details() {
    var v = $("#type").val();
    if (v == "0") {
      $("#li_model").show();
      $("#li_categoryview").show();
      $("#li_contentview").show();
      $("#li_nav").hide();
      $("#navigation").val("");
    }
    else if (v == "1") {
      $("#li_model").hide();
      $("#li_categoryview").show();
      $("#li_contentview").hide();
      $("#contentview").val("");
      $("#li_nav").hide();
      $("#navigation").val("");
    }
    else if (v == "2") {
      $("#li_model").hide();
      $("#li_categoryview").hide();
      $("#categoryview").val("");
      $("#li_contentview").hide();
      $("#contentview").val("");
      $("#li_nav").show();
    }
  }
</script>
@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 

/// <summary>
    /// 修改栏目信息
    /// </summary>
    /// <param name="category"></param>
    /// <returns></returns>
    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("<li>返回<a href='" + url.action("managedetails", "category", new { id = category.categoryid }) + "'>栏目详细资料</a>页面,修改信息后重新操作</li><li>联系网站管理员</li>") };
        return redirecttoaction("manageerror", "prompt", _e);
      }
    }

很简单,首先是判断栏目类型,根据栏目类型清除无关数据,然后将修改保存到数据库。
试一下将“测试栏目”改成“公司简介” 

MVC4制作网站教程第四章 更新栏目4.3

保存成功!

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。