【问题标题】:how to Pass Enum int value to controller如何将 Enum int 值传递给控制器
【发布时间】: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


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:

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

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-03-30
      • 1970-01-01
      • 2013-09-05
      • 1970-01-01
      • 1970-01-01
      • 2012-08-13
      • 1970-01-01
      相关资源
      最近更新 更多