如何将Enum int值传递给控制器

时间:2016-10-12 11:32:50

标签: c# asp.net-mvc linq enums

我的模型中有枚举

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

    }

那么我怎样才能获得1月1日的值,6月6日等值。 帮助我

2 个答案:

答案 0 :(得分:2)

您可以将操作签名更新为以下内容:

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

答案 1 :(得分:1)

Enum基本上只是一个int,因此您可以将SelMonth转换为Months

由于您的SelMonth可以为空,您应该这样做

Months month = (Month)SelMonth.GetValueOrDefault();