MVC4 Making Web Tutorials Chapter III adding user Group operations 3.2_ Practical Tips

Source: Internet
Author: User

First, users

Second, the user group

2.1 Browse User Groups

2.2 Adding user groups

Modify [Add]action

<summary>
    ///Add user group
    ///</summary>
    ///<returns></returns>
    [ Adminauthorize] public
    actionresult Add ()
    {
      viewdata.add ("Type", typeselectlist);
      return View ();
    }

Add Enhanced Type View

After the completion of the code is as follows:

@model ninesky.models.usergroup@{viewbag.title = "Add user group";
Layout = "~/views/layout/_manage.cshtml";
<div class= "Left" > <div class= "Top" ></div> left-hand list </div> <div class= "Split" ></div> <div class= "Workspace" > <div class= "Inside" > <div class= "Notebar" >  Add user group </div> @using (Html.BeginForm ()) {@Html. ValidationSummary (tru E <fieldset> <legend> User group information </legend> @Html. HIDDENFOR (model => model. Usergroupid) <ul> <li> <div class= "Editor-label" > @Html labelfor (model => model .
     Type) </div> <div class= "Editor-field" > @Html. DropDownList ("type") </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 (mod El => model. Description) </div> <div class= "Editor-field" > @Html. Editorfor (model => model. Description) @Html. Validationmessagefor (model => model. 
      Description) </div> </li> <li> <div class= "Editor-label" > </div> <div class= "Editor-field" > <input type= "Submit" value= "save"/> </div> </li&gt
    ;
 </ul> </fieldset>} </div> </div> <div class= "clear" ></div> @section Scripts {

 @Scripts. Render ("~/bundles/jqueryval")}

Modify user group Add Processing action [Add (UserGroup usergroup)]action, completed code

[HttpPost]
  [Adminauthorize]
  Public ActionResult Add (usergroup usergroup)
  {
   usergrouprsy = new Usergrouprepository ();
   if (Usergrouprsy.add (usergroup))
   {
    Notice _n = new Notice {Title = "Add user group succeeded", Details = "You have successfully added [" +usergroup.name + "] User group! ", Dwelltime = 5, Navigationname =" user group list ", Navigationurl = url.action (" list "," UserGroup ")};
    Return redirecttoaction ("Managenotice", "Prompt", _n);
   }
   else
   {
    error _e = new Error {Title = "Add user group failed", Details = "Failed to save to database when adding user group", cause = "System error", Solution = Server . UrlEncode ("<li> return <a href= '" + url.action ("Add", "usergroup") + "' > Add User </a> page, enter correct information and then restart </li ><li> Contact webmaster </li> ")};
    Return redirecttoaction ("Manageerror", "Prompt", _e);
   }
  

View in the browser

Input data test, you can add data.

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.