MVC creates exclusive folders for users and mvc users

Source: Internet
Author: User
Tags asp net

MVC creates exclusive folders for users and mvc users

Assume that you need to create an exclusive folder for the user, the folder name is the user name, and you need to create the target folder under different folders according to the user type.

 

Create the "Users" folder on the drive, and create the "Gold" folder to classify "Gold Members" and "Silver" to classify "Silver members ".

 

About the user's Model.

Using System. componentModel. dataAnnotations; namespace MvcApplication1.Models {public class User {public int Id {get; set;} [Display (Name = "User Type")] [Required (ErrorMessage = "Required")] public short UserType {get; set;} [Display (Name = "username")] [Required (ErrorMessage = "Required")] [StringLength (10, MinimumLength = 2, errorMessage = "2-10 characters in length")] public string UserName {get; set ;}}}

 

 

Create a user type enumeration.

using MvcApplication1.Extension;namespace MvcApplication1.Models{    public enum UserTypeEnum    {        Gold = 0,        Silver = 1    }}

 

 

The user type is displayed in the view as a drop-down box. What should I do if I want to display Chinese characters in the drop-down list?

-- You can set a custom Attribute for the enumerated items, provide an Attribute, and finally read the drop-down box in the view to modify UserTypeEnum.

Using MvcApplication1.Extension; namespace MvcApplication1.Models {public enum UserTypeEnum {[EnumDisplayName ("Gold member")] Gold = 0, [EnumDisplayName ("Silver member")] Silver = 1 }}

 

EnumDisplayNameAttribute

using System;namespace MvcApplication1.Extension{    public class EnumDisplayNameAttribute : Attribute    {        private string _displayName;        public EnumDisplayNameAttribute(string displayName)        {            _displayName = displayName;        }        public string DisplayName        {            get            {                return _displayName;            }        }    }}

In the preceding section, a constructor is provided to assign the Chinese name of the enumeration item to the field _ displayName and the attribute DisplayName to allow the Chinese name of the enumeration item to be accessed.


In addition, Html. dropDownListFor () requires that the Set Data source be of the IEnumerable <SelectListItem> type. Therefore, it is necessary to write a help method to read the Chinese name of the enumeration item and return it as IEnumerable <SelectListItem> type.

Using System; using System. collections. generic; using System. reflection; using System. web. mvc; namespace MvcApplication1.Extension {public class EnumExt {// <summary> // obtain an Attribute value of the custom Attribute of the enumerated member /// </summary> /// <param name = "e"> enumeration member </param> // <returns> </returns> public static string GetEnumDescription (object e) {// obtain the Type object Type t = e. getType (); // get all fields of the Type object FieldInfo [] MS = t. getFields (); // Traverse all fields foreach (FieldInfo f in MS) {if (f. Name! = E. toString () {continue;} if (f. isDefined (typeof (EnumDisplayNameAttribute), true) {return (f. getCustomAttributes (typeof (EnumDisplayNameAttribute), true) [0] as EnumDisplayNameAttribute ). displayName ;}} return e. toString ();} public static List <SelectListItem> GetSelectList (Type enumType) {List <SelectListItem> selectList = new List <SelectListItem> (); // selectList. add (new SelectListItem {Text = "-- select --", Value = ""}); foreach (object e in Enum. getValues (enumType) {selectList. add (new SelectListItem {Text = GetEnumDescription (e), Value = (int) e ). toString ()}) ;}return selectList ;}}}

 

In HomeController.

Using System. IO; using System. web. mvc; using MvcApplication1.Extension; using MvcApplication1.Models; namespace MvcApplication1.Controllers {public class HomeController: Controller {public ActionResult AddUser () {ViewData ["ut"] = EnumExt. getSelectList (typeof (UserTypeEnum); return View () ;}// root folder private const string main_Dir = @ "F:/Users "; [HttpPost] public ActionResult AddUser (User user) {ViewD Ata ["ut"] = EnumExt. getSelectList (typeof (UserTypeEnum); if (ModelState. isValid) {// create the user folder CreateDir (user. userType, user. userName); return Content ("the user exclusive folder is created successfully! ");} Return View (user);} // create a private static void CreateDir (short userType, string subdir) folder Based on the file name and user type) {// path of the folder to be created: string path = ""; switch (userType) {case (short) UserTypeEnum. gold: path = main_Dir + "/" + "Gold/" + subdir; break; case (short) UserTypeEnum. silver: path = main_Dir + "/" + "Silver/" + subdir; break; default: path = main_Dir + "/" + "Silver/" + subdir; break ;} directory. createDirectory (path );}}}

 

In Home/Index. cshtml.

@ Model MvcApplication1.Models. User @ {ViewBag. Title = "AddUser"; Layout = "~ /Views/Shared/_ Layout. cshtml ";}< style type =" text/css "> ul {list-style-type: none ;} </style> 

 

Run:


Click "CREATE ":


A "Darren" folder is added under F: \ Users \ Gold:


How to Create a folder in ASPNET MVC3

String filePhysicalPath = Server. MapPath ("~ /Image/newDir /");
If (! Directory. Exists (filePhysicalPath) // determines whether the upload folder Exists. If not, create
{
Directory. CreateDirectory (filePhysicalPath); // create a folder
}

In this way, the key lies in this "~". Symbol
 
In VS 2010, an asp net mvc project was created. I created a folder in views, created a view under the folder, and

Must comply with MVC conventions
Starting from Controller
The Controller name must end with the Controller name, for example, ProjectController
The folder name under View must be named by the Controller name, such as View/Project.
The View name in the View folder must comply with the ActionResult method name in the Controller. For example, the Index method corresponds to the View/Project/Index. cshtml file.

Related Article

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.