我打算在這里提取男性和女性值的列舉類
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
}
資料訪問層是我從我的資料庫 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;
}
}
}
業務邏輯層不言自明
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
}
一切開始的控制器層
[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);
}
大家好,我想使用我的 Gender 列舉為 AngularJS POST 請求回傳男性或女性,但我一直為男性獲取 0,為女性獲取 1。我該如何解決?評論中的所有其他詳細資訊。
uj5u.com熱心網友回復:
對您來說,列舉由于命名方式而看起來像字串,但實際上它們是數字,至少在計算機上是這樣。你可以打電話.ToString()來讓這個名字引起轟動。
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/377683.html
標籤:C# 网站 angularjs asp.net-mvc-4
