java 用户专属文件夹_MVC为用户创建专属文件夹

假设需要为用户创建专属文件夹,文件夹名为用户名,并且需要根据用户类型在不同的文件夹下创建目标文件夹。

在F盘创建"Users"文件夹,在其中创建"Gold"文件夹,用来归类"金牌会员",创建"Silver",用来归类"银牌会员"。

关于用户的Model。

usingSystem.ComponentModel.DataAnnotations;namespaceMvcApplication1.Models

{public classUser

{public int Id { get; set; }

[Display(Name= "用户类型")]

[Required(ErrorMessage= "必填")]public short UserType { get; set; }

[Display(Name= "用户名")]

[Required(ErrorMessage= "必填")]

[StringLength(10, MinimumLength = 2, ErrorMessage = "长度2-10位")]public string UserName { get; set; }

}

}

创建关于用户类型的枚举。

usingMvcApplication1.Extension;namespaceMvcApplication1.Models

{public enumUserTypeEnum

{

Gold= 0,

Silver= 1}

}

用户类型最终会以下拉框的形式呈现在视图中。如果想在下拉框中显示中文该如何做?

--可以为枚举项打上自定义的Attribute,并提供一个属性,最终被读到视图中的下拉框,修改UserTypeEnum。

usingMvcApplication1.Extension;namespaceMvcApplication1.Models

{public enumUserTypeEnum

{

[EnumDisplayName("金牌会员")]

Gold= 0,

[EnumDisplayName("银牌会员")]

Silver= 1}

}

EnumDisplayNameAttribute

usingSystem;namespaceMvcApplication1.Extension

{public classEnumDisplayNameAttribute : Attribute

{private string_displayName;public EnumDisplayNameAttribute(stringdisplayName)

{

_displayName=displayName;

}public stringDisplayName

{get{return_displayName;

}

}

}

}

以上,提供构造函数以便把枚举项的中文名赋值给字段_displayName,提供属性DisplayName以便把枚举项的中文名最终被访问。

另外,视图中的Html.DropDownListFor()要求集合数据源是IEnumerable类型,所以,有必要写一个帮助方法,把枚举项的中文名读取出来并以IEnumerable类型返回。

usingSystem;usingSystem.Collections.Generic;usingSystem.Reflection;usingSystem.Web.Mvc;namespaceMvcApplication1.Extension

{public classEnumExt

{///

///获取枚举成员的自定义Attribute的一个属性值///

/// 枚举成员

///

public static string GetEnumDescription(objecte)

{//获取枚举成员的Type对象

Type t =e.GetType();//获取Type对象的所有字段

FieldInfo[] ms =t.GetFields();//遍历所有字段

foreach (FieldInfo f inms)

{if (f.Name !=e.ToString())

{continue;

}if (f.IsDefined(typeof(EnumDisplayNameAttribute), true))

{return (f.GetCustomAttributes(typeof(EnumDisplayNameAttribute), true)[0] asEnumDisplayNameAttribute).DisplayName;

}

}returne.ToString();

}public static ListGetSelectList(Type enumType)

{

List selectList = new List();//selectList.Add(new SelectListItem{Text = "--请选择--",Value = ""});

foreach (object e inEnum.GetValues(enumType))

{

selectList.Add(new SelectListItem { Text = GetEnumDescription(e), Value = ((int)e).ToString() });

}returnselectList;

}

}

}

在HomeController中。

usingSystem.IO;usingSystem.Web.Mvc;usingMvcApplication1.Extension;usingMvcApplication1.Models;namespaceMvcApplication1.Controllers

{public classHomeController : Controller

{publicActionResult AddUser()

{

ViewData["ut"] = EnumExt.GetSelectList(typeof(UserTypeEnum));returnView();

}//根文件夹

private const string main_Dir = @"F:/Users";

[HttpPost]publicActionResult AddUser(User user)

{

ViewData["ut"] = EnumExt.GetSelectList(typeof(UserTypeEnum));if(ModelState.IsValid)

{//创建用户文件夹

CreateDir(user.UserType, user.UserName);return Content("用户专属文件夹创建成功!");

}returnView(user);

}//根据文件名和用户类型创建文件夹

private static void CreateDir(short userType, stringsubdir)

{//需要创建的文件夹的路径

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);

}

}

}

在Home/Index.cshtml中。

@model MvcApplication1.Models.User

@{

ViewBag.Title= "AddUser";

Layout= "~/Views/Shared/_Layout.cshtml";

}

list-style-type: none;

}

AddUser

@using (Html.BeginForm("AddUser", "Home", FormMethod.Post, new {id = "addForm"}))

{

  • @Html.LabelFor(u=>u.UserName)

    @Html.EditorFor(u=>u.UserName)

    @Html.ValidationMessageFor(u=>u.UserName)

  • @Html.LabelFor(u=>u.UserType)

    @Html.DropDownListFor(u=> u.UserType, (List)ViewData["ut"],"==选择用户类型==")

    @Html.ValidationMessageFor(u=>u.UserType)

}

运行:

10713f95232d4eb2cbfb32e0a0eb23cd.png

点击"创建":

8d8d567e35e79596ae3121ee8538ef73.png

在F:\Users\Gold下多了一个"Darren"文件夹:

aee6f1f5fc8a5021adb971faa3d88911.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值