0

I have problem and still do not understand the concept about Enum in EnumDropDownListFor to bind spesific Existing Enum value.

public class LeaveRequestModel
{
    public int LeaveId { get; set; }
    public string LeaveCode { get; set; }
    public string Reason { get; set; }
    public LeaveStatus Status { get; set; } 
}
public enum LeaveStatus
{
    [Display(Name = "New Request")]
    NewRequest = 1,
    [Display(Name = "Approved by SPV")]
    ApprovedBySpv = 2,
    [Display(Name = "Approved by HR")]
    ApprovedByHr = 3,
    [Display(Name = "Rejected By SPV")]
    RejectedBySpv = 4
}

I displayed Enum in My View using EnumDropDownListFor

@Html.EnumDropDownListFor(m => m.Status, "-Please select-", new { @class = "col-sm-10", @required = "required" })

and the result is like this:

enter image description here

My Question is How can I display only specific Enum Value ApprovedBySpv and ApprovedByHr in EnumDropDownListFor with specific condition? If it is possible, how can I do it?

mxmissile
  • 11,464
  • 3
  • 53
  • 79
aminvincent
  • 553
  • 1
  • 12
  • 43
  • Did you check these answers? https://stackoverflow.com/questions/27133014/exclude-remove-value-from-mvc-5-1-enumdropdownlistfor – Andrew Jan 06 '20 at 18:39
  • Is hiding them on the browser a valid alternative? Maybe it's an easier approach. – Andrew Jan 06 '20 at 19:21

2 Answers2

1

Model:

public class LeaveRequestModel
{
    ...
    public LeaveStatus Status { get; set; }
    public SelectList FilteredLeaveStatus {get;set;}
}

Controller:

public ActionResult LeaveRequest()
{
  var model = new LeaveRequestModel();

  if(condition)
  {
      var filtered = new[]
      {
          new SelectListItem{Value = LeaveStatus.ApprovedBySpv, Text = "Rejected By SPV"},
          new SelectListItem{Value = LeaveStatus.ApprovedByHr, Text = "Approved by HR"}
      };
    model.FilteredLeaveStatus = new SelectList(filtered);
  }
  ...
  return View(model);
}

View:

@Html.DropDownListFor(m => m.Status, Model.FilteredLeaveStatus, "-Please select-", new { @class = "col-sm-10", @required = "required" })
mxmissile
  • 11,464
  • 3
  • 53
  • 79
1

You can add a method to the class that will take in the values that determine the condition, and only return the relevant enums. For example, let's say one can only advance the status to a higher status, but not move it back to a lower status. You can add a method that will take the current status and only return statuses with a higher value.

Method (add to LeaveRequestModel class)

public IEnumerable<LeaveStatus> GetAvailableStatuses(){
    return Enum.GetValues(typeof(LeaveStatus)).Where(e => e >= Status);
}

View

@Html.EnumDropDownListFor(m => m.LeaveStatus, Model.GetAvailableStatuses().Select(s => new SelectListItem {
    Text = s.ToString(),
    Value = ((int)s).ToString()
}).ToList(), "-Please select-", new { @class = "col-sm-10", @required = "required" })

Note that I took a guess at what your condition might be, but you can update that in the GetAvailableStatuses method.

Aliza Berger
  • 171
  • 1
  • 7