简体   繁体   English

枚举返回 int 而不是字符串 C#

[英]Enum returning int instead of string C#

Enum Class which I intend to extract male and female values here我打算在这里提取男性和女性值的枚举 Class

public class Employee
{
    public int ID { get; set; }
    public string Name { get; set; }
    public DateTime DoB { get; set; }
    public enum MF { Male,Female }
    public MF Gender { get; set; } //here's my target
}

Data Access Layer here's where I extract from my DB msql数据访问层是我从数据库 msql 中提取的地方

public IList<Employee> GetAllEmployees()
{
    string query = "EXEC GetAllEmployees";
    string constring = "Data Source=.\\SQLEXPRESS;Initial Catalog=ProjectDatabase;Integrated Security=True;Pooling=False";
    IList<Employee> AllEmployees = new List<Employee> {};

    using (SqlConnection con = new SqlConnection(constring))
    {
        using (SqlCommand cmd = new SqlCommand(query, con))
    {
        List<Employee> customers = new List<Employee>();
        //cmd.CommandType = CommandType.Text;
        con.Open();
        using (SqlDataReader sdr = cmd.ExecuteReader())
    {
        while (sdr.Read())
       {
           AllEmployees.Add(new Employee
           {
              ID     = Convert.ToInt32(sdr["ID"]),
              Name   = sdr["Name"].ToString(),
              DoB    = (DateTime)sdr["DoB"],
              Gender = (MF)Enum.Parse(typeof(MF), (string)sdr["Gender"]), //here's where it extracts the Gender value as 0 or 1 instead in plain words
           });
                }
                    }
                    con.Close();
                    return AllEmployees;
                }
            }
        }

Business Logic Layer self explanatory业务逻辑层不言自明

        public IList<Employee> GetEmployees(string name,MF gender)
        {
            EmployeeDAL EDAL = new EmployeeDAL();

            if (name == "")
                return EDAL.GetAllEmployees(); //Ignore this for now

            else
                return EDAL.GetFilteredEmployees(name,gender); //this gets used in this case
        }

Controller Layer where it all starts Controller 一切开始的层

[Route("GetEmployees")]
[HttpPost]
public IList<Employee> GetEmployees(JArray PostData)
{
   string Name = (string)PostData[0]["Name"];
   MF Gender = (MF)Enum.Parse(typeof(MF), (string)PostData[0]["Gender"]);//grabed from a post request from AngularJS code
   EmployeeBLL EBLL = new EmployeeBLL();
   return EBLL.GetEmployees(Name,Gender);
}

Hello all, I would like to use my Gender enum to return male or female for an AngularJS POST request, but I keep getting 0 for male and one for female.大家好,我想使用我的性别枚举为 AngularJS POST 请求返回男性或女性,但我一直得到男性和女性的 0。 How do I fix this?我该如何解决? All additional details in the comments.评论中的所有其他详细信息。

To you Enums look like strings due to the way they are named, but in truth they are numbers, at least according to a computer.对你来说,枚举看起来像字符串,因为它们的命名方式,但实际上它们是数字,至少根据计算机是这样。 You can call .ToString() to get the name as a stirng.您可以调用.ToString()来获取名称作为搅拌。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM