C# 如何在编辑记录中设置EnumDropDownListFor属性的保存值
我已经为血型创建了枚举 我有一个问题,即如何设置控制器中血型字段从数据库中获取的值。如何在控制器的编辑操作中设置枚举成员组=?的值 我正在s.BloodGroup 枚举 模型 控制器 看法C# 如何在编辑记录中设置EnumDropDownListFor属性的保存值,c#,asp.net-mvc,asp.net-mvc-4,enums,C#,Asp.net Mvc,Asp.net Mvc 4,Enums,我已经为血型创建了枚举 我有一个问题,即如何设置控制器中血型字段从数据库中获取的值。如何在控制器的编辑操作中设置枚举成员组=?的值 我正在s.BloodGroup 枚举 模型 控制器 看法 @LabelFor(model=>model.Enum_Member_BloodGroup,新的{@class=“controllabel col-md-3”}) @Html.EnumDropDownListFor(model=>model.Enum\u Member\u BloodGroup,“--Sel
@LabelFor(model=>model.Enum_Member_BloodGroup,新的{@class=“controllabel col-md-3”})
@Html.EnumDropDownListFor(model=>model.Enum\u Member\u BloodGroup,“--Select-->”)
您必须先将数据库中的int
值显式转换为Enum
类型,然后再进行分配,在视图中,html助手将自动选择该值
这样做:
select new EditStudentViewModel
{
Student_ID = s.Student_ID,
FullName = s.FullName,
Standard = s.Standard,
RollNo = s.RollNo,
Enum_Member_BloodGroup = (clsEnums.bloodGroup)s.BloodGroup
如果是在“模型”属性中,则应将其设置为“由辅助对象自动选择”
public class EditStudentViewModel
{
[DisplayName("Blood Group")]
public clsEnums.bloodGroup Enum_Member_BloodGroup { get; set; }
[HttpGet]
public ActionResult Edit(int id = 0)
{
try
{
using (TakshShilla_SchoolEntities objConnection = new TakshShilla_SchoolEntities())
{
var str = (from s in objConnection.tblStudents
where s.Student_ID == id
select new EditStudentViewModel
{
Student_ID = s.Student_ID,
FullName = s.FullName,
Standard = s.Standard,
RollNo = s.RollNo,
Enum_Member_BloodGroup =
<div class="form-group">
@Html.LabelFor(model => model.Enum_Member_BloodGroup, new { @class = "control-label col-md-3" })
<div class="col-md-9">
@Html.EnumDropDownListFor(model => model.Enum_Member_BloodGroup, "--Select--")
</div>
</div>
select new EditStudentViewModel
{
Student_ID = s.Student_ID,
FullName = s.FullName,
Standard = s.Standard,
RollNo = s.RollNo,
Enum_Member_BloodGroup = (clsEnums.bloodGroup)s.BloodGroup