根据条件在ASP.NET MVC中选择特定的Enum以用于EnumDropDownListFor。

huangapple go评论96阅读模式
英文:

Select Specific Enum to EnumDropDownListFor based on Condition in ASP.NET MVC

问题

我有问题,仍然不理解关于EnumDropDownListForEnum的概念,以绑定特定的现有Enum值。

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
}

我使用EnumDropDownListFor在我的视图中显示枚举值。

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

结果如下:

根据条件在ASP.NET MVC中选择特定的Enum以用于EnumDropDownListFor。

我的问题是如何在EnumDropDownListFor中仅显示特定的Enum值ApprovedBySpvApprovedByHr,并根据特定条件进行选择?如果可能的话,我应该如何做?

英文:

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:

根据条件在ASP.NET MVC中选择特定的Enum以用于EnumDropDownListFor。

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?

答案1

得分: 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 = "被主管拒绝" },
            new SelectListItem { Value = LeaveStatus.ApprovedByHr, Text = "人力资源已批准" }
        };
        model.FilteredLeaveStatus = new SelectList(filtered);
    }
    ...
    return View(model);
}

View:

@Html.DropDownListFor(m => m.Status, Model.FilteredLeaveStatus, "-请选择-", new { @class = "col-sm-10", @required = "required" })

(Note: I've translated the SelectListItem text values in the Controller to Chinese as per your request.)

英文:

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

答案2

得分: 1

你可以向该类添加一个方法,该方法将接受决定条件的值,并仅返回相关的枚举值。
例如,假设只能将状态提升到更高的状态,而不能将其降级到更低的状态。您可以添加一个方法,该方法将获取当前状态并仅返回具有较高值的状态。

方法(添加到LeaveRequestModel类)

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

视图

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

请注意,我猜测了您的条件可能是什么,但您可以在GetAvailableStatuses方法中进行更新。

英文:

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&lt;LeaveStatus&gt; GetAvailableStatuses(){
    return Enum.GetValues(typeof(LeaveStatus)).Where(e =&gt; e &gt;= Status);
}

View

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

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

huangapple
  • 本文由 发表于 2020年1月7日 01:52:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/59616710.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定