所以我需要創建一個具有動態屬性的模型;即,此屬性可以是 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
}
}
我知道我寫的東西是錯誤的,但理解我的問題,我如何使代碼明智?
uj5u.com熱心網友回復:
您可以創建一個列舉AttributeConditionType具有四個成員:enGoodBad,enYesNo,enMajorMinor和enMissing。并修改 Attribute 類如下:
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();
}
結果如下:

轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/337112.html
標籤:C# asp.net asp.net-mvc 实体框架 asp.net核心
下一篇:物體框架在插入后填充相關記錄
