Saurabh Solanki
Saurabh Solanki

Reputation: 2204

how to Pass Enum int value to controller

I have enum in my model

  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
    }

View for display month names

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

now i want the int value for the enum month selected from the form in controller

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

    }

so how can i get int value like 1 for january, 6 for june etc. help me

Upvotes: 2

Views: 3495

Answers (2)

rmorrin
rmorrin

Reputation: 2495

You can update your action signature to the following:

public ActionResult ViewDetails(int UserId, string currentFilter, OSCTrackWeb.Models.AttendanceMaster.Months SelMonth, int? SelYear, string searchString, int? Page_No)

MVC will bind the submitted value to your enum itself. If nothing is provided, the value of SelMonth will default to to 0 (despite the fact you have not defined an enum value for 0).

If you need to, you can check if the provided value is defined in your enum with

Enum.IsDefined(typeof(OSCTrackWeb.Models.AttendanceMaster.Months), SelMonth);

Upvotes: 2

Paweł Hemperek
Paweł Hemperek

Reputation: 1180

Enum is essentially just an int so you can cast your SelMonth to Months.

Since your SelMonth is nullable, you should do this like that

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

Upvotes: 1

Related Questions