如何在编辑记录中设置EnumDropDownListFor属性的保存值



我已经为血型创建了Enum。

我有一个问题,即如何设置我从数据库中获得的控制器中的血型字段值。如何在控制器的编辑操作中设置Enum_Member_BloodGroup = ?的值。

我在s中获得数据。BloodGroup

Enum

public enum bloodGroup
{
        [Display(Name = "A +ve")]
        Apositive = 1,
        [Display(Name = "B +ve")]
        BPostive,
        [Display(Name = "AB +ve")]
        ABPositive,
        [Display(Name = "O +ve")]
        OPositive,
        [Display(Name = "A -ve")]
        ANegative,
        [Display(Name = "B -ve")]
        BNegative,
        [Display(Name = "AB -ve")]
        ABNegative,
        [Display(Name = "O -ve")]
        ONegative
}

模型
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>

在分配之前,您必须将来自数据库的int值显式转换为您的Enum类型,并且在视图中它将由html helper自动选择。

这样做:

select new EditStudentViewModel
           {
              Student_ID = s.Student_ID,
              FullName = s.FullName,
              Standard = s.Standard,
              RollNo = s.RollNo,                                  
              Enum_Member_BloodGroup = (clsEnums.bloodGroup)s.BloodGroup

相关内容

  • 没有找到相关文章

最新更新