【发布时间】:2016-10-12 11:32:50
【问题描述】:
我的模型中有枚举
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>
现在我想要从控制器中的表单中选择枚举月份的 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;
}
}
那么我怎样才能得到 int 值,比如 1 代表 1 月,6 代表 6 月等等。 帮帮我
【问题讨论】:
-
你为什么要这样做 - 它是一个
enum,所以你的方法中的参数需要是Month? SelMonth,而不是int? SelMonth -
因为我想选择那个月份的 int enum,所以我可以在 linq 查询中比较它,因为 linq 查询不允许任何转换
-
你在查询之前强制转换 - var mth = (int SelMonth);` 并在查询中使用
mth:) -
好的 ...谢谢这对我有用
标签: c# asp.net-mvc linq enums