C# 如何将枚举int值传递给控制器
我的模型中有enumC# 如何将枚举int值传递给控制器,c#,asp.net-mvc,linq,enums,C#,Asp.net Mvc,Linq,Enums,我的模型中有enum public enum Months { January = 1, February = 2, March = 3, April = 4, May = 5, June = 6, July = 7, August = 8, September = 9, October = 10, Nove
public enum Months
{
January = 1,
February = 2,
March = 3,
April = 4,
May = 5,
June = 6,
July = 7,
August = 8,
September = 9,
October = 10,
November = 11,
December = 12
}
显示月份名称的视图
<div class="left row">
@using (Html.BeginForm("ViewDetails", "Employees", FormMethod.Post))
{
<input type="hidden" id="UserId" name="UserId" value="@ViewBag.UserId" />
<div class="col-sm-4">
<div class="col-sm-6">
@Html.DropDownList("SelMonth", new SelectList(Enum.GetValues(typeof(OSCTrackWeb.Models.AttendanceMaster.Months))), "Select Month", new { @class = "form-control" })
</div>
<div class="col-sm-6">
@Html.DropDownList("SelYear", Enumerable.Range(DateTime.Now.Year, 10).Select(x => new SelectListItem { Text = x.ToString() }), "Select Year", new { @class = "form-control" })
</div>
</div>
<input class="btn btn-primary" type="submit" value="Filter" />
<input class="btn btn-info" type="button" value="Reset" onclick="return Reset();" />
}
</div>
@使用(Html.BeginForm(“ViewDetails”、“Employees”、FormMethod.Post))
{
@DropDownList(“selmount”,新的选择列表(Enum.GetValues(typeof(OSCTrackWeb.Models.AttendanceMaster.Months)),“选择月”,新的{@class=“form control”})
@DropDownList(“SelYear”,Enumerable.Range(DateTime.Now.Year,10)。选择(x=>new SelectListItem{Text=x.ToString()}),“选择年份”,新建{@class=“form control”})
}
现在,我希望从controller中的表单中选择枚举月份的int值
public ActionResult ViewDetails(int UserId, string currentFilter, int? SelMonth, int? SelYear, string searchString, int? Page_No)
{
int Size_Of_Page = 10;
int No_Of_Page = (Page_No ?? 1);
if (searchString != null)
{
No_Of_Page = 1;
}
else
{
searchString = currentFilter;
}
ViewBag.CurrentFilter = searchString;
ViewBag.UserId = UserId;
if (UserId != null)
{
if (SelMonth == null && SelYear == null)
{
SelMonth = DateTime.Now.Month;
SelYear = DateTime.Now.Year;
List<AttendanceMaster> amobj = db.AttendanceMasters.Where(s => s.EmpId == UserId && s.AttendanceDate.Value.Month == SelMonth && s.AttendanceDate.Value.Year == SelYear).ToList();
ViewBag.Name = LoggedUser.Name;
IPagedList<AttendanceMaster> lstdtl = amobj.ToPagedList(No_Of_Page, Size_Of_Page);
return View("ViewDetails", lstdtl);
}
else
{
List<AttendanceMaster> amobj = db.AttendanceMasters.Where(s => s.EmpId == UserId && s.AttendanceDate.Value.Month == SelMonth && s.AttendanceDate.Value.Year == SelYear).ToList();
ViewBag.Name = LoggedUser.Name;
IPagedList<AttendanceMaster> lstdtl = amobj.ToPagedList(No_Of_Page, Size_Of_Page);
return View("ViewDetails", lstdtl);
}
}
else
{
return null;
}
}
public ActionResult视图详细信息(int UserId、string currentFilter、int?selmount、int?SelYear、string searchString、int?页码)
{
整页的整数大小=10;
第页的整数=(第1页);
if(searchString!=null)
{
第页中没有第页=1;
}
其他的
{
searchString=currentFilter;
}
ViewBag.CurrentFilter=搜索字符串;
ViewBag.UserId=用户ID;
if(UserId!=null)
{
如果(SelMonth==null&&SelYear==null)
{
selmount=DateTime.Now.Month;
SelYear=DateTime.Now.Year;
列出amobj=db.AttendanceMasters.Where(s=>s.EmpId==UserId&&s.AttendanceDate.Value.Month==SelMonth&&s.AttendanceDate.Value.Year==SelYear)。ToList();
ViewBag.Name=LoggedUser.Name;
IPagedList lstdtl=amobj.ToPagedList(页面的编号、页面的大小);
返回视图(“视图详细信息”,lstdtl);
}
其他的
{
列出amobj=db.AttendanceMasters.Where(s=>s.EmpId==UserId&&s.AttendanceDate.Value.Month==SelMonth&&s.AttendanceDate.Value.Year==SelYear)。ToList();
ViewBag.Name=LoggedUser.Name;
IPagedList lstdtl=amobj.ToPagedList(页面的编号、页面的大小);
返回视图(“视图详细信息”,lstdtl);
}
}
其他的
{
返回null;
}
}
那么我怎样才能得到int值,比如1月的1,6月的6,等等。
帮助我
Enum
本质上只是一个int
,因此您可以将selmount
转换为Months
由于您的selmount
可为空,因此您应该这样做
Months month = (Month)SelMonth.GetValueOrDefault();
您可以将您的操作签名更新为以下内容:
public ActionResult ViewDetails(int UserId, string currentFilter, OSCTrackWeb.Models.AttendanceMaster.Months SelMonth, int? SelYear, string searchString, int? Page_No)
MVC将把提交的值绑定到枚举本身。如果未提供任何内容,SelMonth的值将默认为0(尽管您尚未为0定义枚举值)
如果需要,可以使用检查在枚举中是否定义了提供的值
Enum.IsDefined(typeof(OSCTrackWeb.Models.AttendanceMaster.Months), SelMonth);
你到底为什么要这样做?它是一个
enum
,所以你的方法中的参数必须是Month?选择月份,而不是整数?selmount
因为我希望选中该月份的int-enum,所以我可以在linq查询中比较它,因为linq查询不允许任何强制转换您在查询之前强制转换它-var mth=(int-selmount);`并在查询中使用mth
:)OK…谢谢您为我工作