动态分配模型 属性 类型

dynamically allocating Model property type

所以我需要创建一个具有动态 属性 的模型;即,此 属性 可以是 3 种类型的枚举中的任何一种,并且是在模型创建时动态分配的

我的模特:

public class Attribute
{
    public int AttributeId { get; set; }

    public AttributeConditionType Condition { get; set; } = enGoodBad;

}

我的动态类型:

public class AttributeConditionType
{
    public enum enGoodBad
    {
        Good,
        Bad, 
        Excellent
    }

    public enum enYesNo
    {
        Yes,
        No
    }

    public enum enMajorMinor
    {
        Major,
        Minor, 
    }

    public enum enMissing
    {
        None,
        Some,
        One,
        Many
    }
}

我知道我写的是错误的,但了解我的问题,我如何才能使它成为可能的代码?

您可以创建一个具有四个成员的枚举 AttributeConditionTypeenGoodBadenYesNoenMajorMinorenMissing。并修改属性 class 如下:

public class TestAttribute
{
    public int AttributeId { get; set; }

    // this property is used to store the select AttributeConditionType type.
    public AttributeConditionType Condition { get; set; } = AttributeConditionType.enGoodBad;

    //this property is used to store the select AttributeConditionType's selected value, such as: Good, Bad, Excellent, Yes or No
    public string SelectType { get; set; } 

}

public enum AttributeConditionType
{
    enGoodBad,
    enYesNo,
    enMajorMinor,
    enMissing
}
public enum enGoodBad
{
    Good,
    Bad,
    Excellent
}
public enum enYesNo
{
    Yes,
    No
}
public enum enMajorMinor
{
    Major,
    Minor,
}
public enum enMissing
{
    None,
    Some,
    One,
    Many
}

然后,当您添加 TestAttribute 时,您可以使用级联下拉列表来显示 AttributeConditionType 和关联类型。

这样的代码:

@model Core5_0MVC.Models.TestAttribute

@{
    ViewData["Title"] = "AddAttribute";
}
  
<div class="row">
    <div class="col-md-4">
        <form asp-action="AddAttribute">
            <div asp-validation-summary="ModelOnly" class="text-danger"></div>
            <div class="form-group">
                <label asp-for="AttributeId" class="control-label"></label>
                <input asp-for="AttributeId" class="form-control" />
                <span asp-validation-for="AttributeId" class="text-danger"></span>
            </div>
            <div class="form-group">
                <label asp-for="Condition" class="control-label"></label>
                <select asp-for="Condition" class="form-control"
                        asp-items="Html.GetEnumSelectList<AttributeConditionType>()">
                    <option value="0">Select type ...</option>
                </select>
                <span asp-validation-for="Condition" class="text-danger"></span>
            </div>
            <div class="form-group">
                <label asp-for="SelectType" class="control-label"></label>
                <select asp-for="SelectType" class="form-control">

                </select>
                <span asp-validation-for="SelectType" class="text-danger"></span>
            </div>
            <div class="form-group">
                <input type="submit" value="Create" class="btn btn-primary" />
            </div>
        </form>
    </div>
</div>

@section Scripts {
    @{await Html.RenderPartialAsync("_ValidationScriptsPartial");}
<script>
    $(function (){
        $("#Condition").change(function () {
            var value = $("#Condition option:selected").text();
            $.ajax({
                url: "/Home/GetSubEnum",
                data: { "type": value },
                method: "Post",
                success: function (response) {
                    //clear the SelectType dropdownlist.
                    $("#SelectType").empty();
                    //add new items in the dropdownlist.
                    $.each(response, function (index, item) {
                        $("#SelectType").append("<option value=" + item.text + ">" + item.text + "</option>");
                    });
                }
            });
        });
    });
</script>

}

家庭控制器:

    public IActionResult AddAttribute()
    {
        return View();
    }
    [HttpPost]
    public IActionResult AddAttribute(TestAttribute attribute)
    {
        return View();
    }

    [HttpPost]
    public IActionResult GetSubEnum(string type)
    {
        var result = new List<SelectListItem>();

        switch (type)
        { 
            case "enYesNo":
                result = GetEnumSelectList<enYesNo>();
                break;
            case "enMajorMinor":
                result = GetEnumSelectList<enMajorMinor>();
                break;
            case "enMissing":
                result = GetEnumSelectList<enMissing>();
                break;
            default:
                result = GetEnumSelectList<enGoodBad>();
                break;
        }

        return Json(result);
    }

    //Convert the Enum to select list.
    public static List<SelectListItem> GetEnumSelectList<T>()
    {
        return (Enum.GetValues(typeof(T)).Cast<T>().Select(
            enu => new SelectListItem() { Text = enu.ToString(), Value = enu.ToString() })).ToList();
    }

结果如下: