设置在Razor下拉列表中选择的枚举值

时间:2014-02-20 10:40:12

标签: asp.net-mvc-3 razor enums

我正在使用剃刀视图来显示考试的得分,它包含一个枚举值,该值包含3个值“通过”,“失败”,“缺席”,我想相应地选择它。我使用的模型是

模型

公共类VerifyResultModel     {         [显示(名称=“StudnetId”)]         public int StudentId {get;组; }

    [Display(Name = "Student")]
    [Required]
    public string Student { get; set; }

    [Display(Name = "Mark")]
    [Required]
    public int Mark { get; set; }

    [Display(Name = "Score")]
    [Required]
    public string Score { get; set; }

    [Display(Name = "Result")]
    public App.EnumValues.ExamResultStatus Result { get; set; }

}  

控制器

    [HttpGet]
    public ActionResult Verify(int Id)
    {
        List<VerifyResultModel> model_result = new List<VerifyResultModel>();
        VerifyResultModel _resultItem;
        foreach (exammark item in marks)
        {
            SchoolApp.EnumValues.ExamResultStatus result = SchoolApp.EnumValues.ExamResultStatus.Absent;
            if(item.Mark >= MinMark)
            {
               result= SchoolApp.EnumValues.ExamResultStatus.Pass;
            }
            else
            {
                result = App.EnumValues.ExamResultStatus.Fail;
            }
            _resultItem = new VerifyResultModel { StudentId = (int)item.StudentId, Student = item.studentmaster.StudentName, Mark = (int)item.Mark, Score = item.Mark.ToString(), Result = result };
            model_result.Add(_resultItem);
        }
         LoadResultsDropdown();
        return View(model_result);
    }
   private void LoadResultsDropdown()
    {
        var types = (from App.EnumValues.ExamResultStatus type in Enum.GetValues(typeof(SchoolApp.EnumValues.ExamResultStatus))
                     select new { Id = type.ToString(), Name = type.ToString() }).ToList();

        ViewBag.ResultList = new SelectList(types, "Id", "Name");
    }

视图

 @model IList<SchoolApp.ViewModels.VerifyResultModel>  
<tbody>
 @for (int item = 0; item < Model.Count(); item++)
 {
     <tr>
     <td>          
        @Html.DisplayFor(modelItem => Model[item].Student)
     </td>
     <td>
       @Html.DisplayFor(modelItem => Model[item].Mark)
     </td>*@
     <td>
       @Html.DisplayFor(modelItem => Model[item].Score)
     </td>
     <td>       
        @Html.DropDownListFor(model => model[item].Result, (SelectList)ViewBag.ResultList) //Here All values are showing as Pass ( first item in dropdown)
     </td>
     </tr>
 }
 </tbody>

问题是即使我传递值Fail / Absent枚举它在组合框中显示为Pass。我怎样才能显示正确的值?

1 个答案:

答案 0 :(得分:2)

请你试试这个:

private void LoadResultsDropdown()
    {
        var types = (from App.EnumValues.ExamResultStatus type in Enum.GetValues(typeof(SchoolApp.EnumValues.ExamResultStatus))
                     select new { Id = type.ToString(), Name = type.ToString() }).ToList();

       var  types=Enum.GetValues(typeof(ExamResultStatus)).Cast<ExamResultStatus>();
        var EnumData= types.Select(c => new { c.Key, c.Value });
        ViewBag.ResultList =  new SelectList(types.AsEnumerable(), "key", "value");
    }

希望这会有所帮助..