MVC4 Making Web Tutorials Fourth Chapter Update column 4.3_ practical skills

Source: Internet
Author: User

Order
First, users
Second, the user group
Third, column
3.1 Add columns
3.2 Browse Columns
3.3 Update Column

The last time you clicked the column name in the tree list, jump to the details page ~/category/managedetails/id. In the detailed page to modify, to complete the column data modification.

First Open "Categorycontroller" to add [managedetails (int id)]action

<summary>///Column Details///</summary>///<param name= "id" ></param>///<ret 
      Urns></returns> public actionresult managedetails (int id) {categoryrsy = new categoryrepository ();
      var _node = categoryrsy.find (ID); if (_node = = null) {Error _e = new Error {Title = "column does not exist", Details = "column does not exist", cause = Server.URLEncode ("&L t;li> column has been deleted </li> "), Solution = Server.URLEncode (" <li> return <a href= ' "+ url.action (" Manage "," Cayegory "
        ) + "' > Column Management </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); }

Code first to see whether the column exists, there is no jump to the error page, followed by the addition of "Model" and "Type" ViewData
Right-click to add enhanced Type View managedetails.cshtml, basic content similar to manageadd.cshtml

@model Ninesky.Models.Category @{viewbag.title = "column Information";
Layout = "~/views/layout/_manage.cshtml"; } <div class= "Workspace" > <div class= "Inside" > <div class= "Notebar" >  Column information </div> @using (Html.BeginForm (" Manageupdate "," Category ") ) {@Html. ValidationSummary (True) <fieldset> <legend> detailed information </legend> ;ul> @Html. Hiddenfor (model => model. CategoryID) <li> <div class= "Editor-label" > @Html. Labelfor (Model => MoD El.  CategoryID) </div> <div class= "Editor-field" > @Html. Displaytextfor (model => model. CategoryID) </div> </li> <li> <div class= "Editor-label" &G
              T @Html. Labelfor (model => model. Name) </div> &Lt;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. Labe Lfor (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 =& Gt 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= "Ed Itor-label "> @Html labelfor (model => model. Navigation) </div> <div class= "Editor-field" > @Html. Editorfor (Model =&gt ; Model. Navigation) @Html. VALIDATIONMESSAGEFOR (model => model. Navigation) </div> </li> <li> <div class= "Editor-label" &G
              T @Html. Labelfor (model => model. Order) </div> <div class= "Editor-field" > @Html. Editorfor (Model => MoD El. Order) @Html. Validationmessagefor (model => model.
              Order) </div> </li> <li> <div class= "Editor-label" > <Input id= "Submit1" type= "Submit" value= "modified"/> </div> <div class= "Editor-field" > 
  </div> </li> </ul> </fieldset>} </div> </div> <div class= "left" > <div class= "Top" ></div> @Html. Action ("Managepartialt Ree "," 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") @Script

 S.render ("~/bundles/jqueryval")}

Note that the @using (Html.BeginForm ("Manageupdate", "Category")), which indicates that the point modification button is followed by submitting the data to the manageupdate. Let's start with this action.

Add the HttpPost way to "Categorycontroller" [manageupdate]action

<summary>///Modify column information///</summary>///<param name= "category" ></param>///& Lt;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 = "Modify column Success", Details = "Modify column success!" ", Dwelltime = 5, Navigationname =" column Details ", Navigationurl = Url.action (" Managedetails "," Category ", new {id = category.c
        Ategoryid})};
      Return redirecttoaction ("Managenotice", "Prompt", _n);
    } else {    Error _e = new Error {Title = "Modify column failure", details = "In modifying column information, failed to save to the database", cause = "System error", Solution = Server.URLEncode ("& lt;li> returns <a href= ' "+ url.action (" Managedetails "," Category ", new {id = Category.
        CategoryID}) + "' > Column Details </a> page, modify information after re-operation </li><li> Contact webmaster </li>")};
      Return redirecttoaction ("Manageerror", "Prompt", _e);

 }
    }

Very simple, first is to judge the column type, according to the column type clear irrelevant data, and then save the changes to the database.
Try to change the "test column" to "Company Profile"

Save success!

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.