简体   繁体   English

下拉列表中允许的属性值 C# ASP.NET MVC

[英]Allowed values of attribute in drop down list C# ASP.NET MVC

I have implemented for the allowed dynamic values for status:我已经实现了允许的状态动态值:

public class EmployeeStatusAttribute : ValidationAttribute
{
    private string[] _allowedValues;

    public EmployeeStatusAttribute(string[] allowedValues)
    {
        _allowedValues = allowedValues;
    }

    protected override ValidationResult IsValid(object value, ValidationContext validationContext)
    {
        var employee = value as Employee;
        if (_allowedValues.Contains(employee.Status))
        {
            return ValidationResult.Success;
        }
        return new ValidationResult(`{employee.Status} is not a valid status`);
    }
}
public class Employee
{
    [EmployeeStatus("Active", "Inactive")]
    public string Status { get; set; }
}

I need to access these allowed values (Active and Inactive).我需要访问这些允许的值(活动和非活动)。 How to access these in view/how to pass these values from the controller to view?如何在视图中访问这些/如何将这些值从 controller 传递给查看?

Currently, I am using the following code in the view目前,我在视图中使用以下代码

<select class="form-control" asp-for="Status">
    <option>Active</option>
    <option>Inactive</option>
</select>

Instead of hardcoding these values.而不是硬编码这些值。 I want to access these dynamically.我想动态访问这些。

in your controller action method在您的 controller 操作方法中

List<SelectListItem> li = new List<SelectListItem>();
li.Add(new SelectListItem { Text = "Active", Value = "1" });
li.Add(new SelectListItem { Text = "Inactive", Value = "0" });
ViewBag.Status_ = li;

in page在页面中

<div class="col-md-10">
   @Html.DropDownList("Status", ViewBag.Status_ as List<SelectListItem>, new { @class = "form-control" })
   <div>
       @Html.ValidationMessageFor(m => m.Status, "", new { @class = "text-danger" })
   </div>
</div>

According to your description, I suggest you could try to use reflection to get the attribute value and then build a list selelctitems.根据您的描述,我建议您可以尝试使用反射来获取属性值,然后构建一个列表选择项。 Then you could use asp-items attribute to create the opinion.然后你可以使用 asp-items 属性来创建意见。

More details, you could refer to below codes:更多细节,您可以参考以下代码:

Model: Model:

public class Employee
{
    [EmployeeStatus("Active", "Inactive","test")]
    public string Status { get; set; }
}

Controller: Controller:

    public IActionResult Index()
    {

         //get the property custom attribute value
         var result = ((EmployeeStatusAttribute)(typeof(Employee).GetProperty("Status").GetCustomAttribute(typeof(EmployeeStatusAttribute))))._allowedValues;

        var ListSelectItem = new List<SelectListItem>();
         //create select list item
        foreach (var item in result)
        {
            ListSelectItem.Add(new SelectListItem() { Text=item,Value=item });

        }
        ViewData["List"] = ListSelectItem;


        var employeetest = new Employee() { Status="Test" };

        return View(employeetest);
    }

View:看法:

<select asp-for="Status" asp-items="@((List<SelectListItem>)ViewData["List"])">
 </select>

Result:结果:

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM