定义枚举
public enum Enum_Level
{
A,
B,
C,
D
}
定义实体
public class ParamModel
{
[EnumDataType(typeof(Enum_Level))]
public Enum_Level? Level { get; set; }
}
控制器验证实体
[AllowAnonymous]
public ActionResult Index(ParamModel model)
{
//ModelState.IsValid 来验证上面实体添加的特性
//如果传入的 Level 参数不在 A、B、C、D 之中就会返回 false
if (!ModelState.IsValid)
{
return RedirectToAction("Index", "Home");
}
return View();
}