I have a field in Model(tablaMetadata) with boolian field for gender as below: (I work with database first MVC)
[DisplayName("gender")]
[Display(Name = "gender")]
public Nullable<bool> EmpSex { get; set; }
I want to get value from EmpSex as "Male" or "Female" by dropdownlist , then convert it to boolean (for posting form to database). I define Enum as below:
public enum gender
{
Male=1,
Female=0
}
I don't Know How can I use htmlhelper for Enumdropdownlist and convert string value of dropdownlist to boolean. Can you help me to define dropdownlist for Enum and converting values?
In your view you can create the dropdown like this
Create the list from enum
like this
@{
var genderList = Enum.GetValues(typeof(Gender)).OfType<Gender>().Select(m => new { Text = m.ToString(), Value = (int)m }).ToList();
}
and create the DropDown like this
@Html.DropDownList("EmpSex", new SelectList(genderList, "Value", "Text", Model.EmpSex))
or
@Html.DropDownListFor(model => model.EmpSex, new SelectList(genderList, "Value", "Text", Model.EmpSex))