英文:
Select Specific Enum to EnumDropDownListFor based on Condition in ASP.NET MVC
问题
我有问题,仍然不理解关于EnumDropDownListFor中Enum的概念,以绑定特定的现有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" })
结果如下:
我的问题是如何在EnumDropDownListFor中仅显示特定的Enum值ApprovedBySpv
和ApprovedByHr
,并根据特定条件进行选择?如果可能的话,我应该如何做?
英文:
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:
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<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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论