1
votes

I am using a razor view for showing the scored of an examination and it contains an enum value that holds 3 values "pass","Fail","absent" and i want to choose it accordingly. The model i used is

model

public class VerifyResultModel { [Display(Name = "StudnetId")] public int StudentId { get; set; }

    [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; }

}  

Controller

    [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");
    }

View

 @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>

The problem is even if i pass values Fail / Absent to enum it shows as Pass in combobox . How can i show the correct value ?

1
@Jmat..where have you added you enum values into viewBag - Sai Avinash
@Jamt..can you put in the code for LoadResultsDropdown() ? - Sai Avinash
Now edited there is a function LoadResultsDropdwon() for doing that . The values Pass Fail Absent are showing in drodown but all are filled with value Pass( even the model property is Fail ) - Sebastian
There is one more limitation as well , i am creating n no of ModelItems and for every Item i have to show the Dropdown , so setting i dont know how can i set the selected value while calling LoadResultsDropdown() - Sebastian

1 Answers

2
votes

Can you please try this:

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");
    }

Hope this helps..