我有一个对象“个人操作”,其中包含一个“四分之一”属性和一个枚举
public int QuarterId { get; set; }
public QuarterList Quarter { get; set; }
public enum QuarterList
{
[Display(Name = "Quarter One")]
Q1 = 1,
[Display(Name = "Quarter Two")]
Q2 = 2,
[Display(Name = "Quarter Three")]
Q3 = 3,
[Display(Name = "Quarter Four")]
Q4 = 4,
}
我在控制器中为此对象创建了一条路由,该路由传递了一个对象(PerformanceManagementRecord)和一个int参数(四分之一)
// GET: Objectives/Create
public IActionResult Create(PerformanceManagementRecord performanceManagementProcess, int quarter)
{
ViewBag.PerformanceManagementProcessId = performanceManagementProcess.Id;
ViewBag.QuarterId = quarter;
ViewBag.UserName = User.Identity.Name;
PopulateStrategicActionDropDownList();
return View();
}
然后我的视图中有一个选择列表,它代表一个季度的枚举列表
<div class="col-md-4">
<form asp-action="Create">
<div asp-validation-summary="ModelOnly" class="text-danger"></div>
<div class="form-group">
<label asp-for="Quarter" class="control-label"></label>
<select asp-for="QuarterId" class="form-control"
asp-items="Html.GetEnumSelectList<IndividualAction.QuarterList>()" ></select>
<span asp-validation-for="Quarter" class="text-danger"></span>
</div>
如何将选择列表绑定到四分之一值参数-该创建方法的URL包含我要设置的值(在本例中为3)
https://localhost:5001/IndividualActions/Create/12?quarter=3
但该值默认为第一季度
答案 0 :(得分:0)
此link中概述的解决方案概述了ASP.NET中的选择列表,该解决方案是使用视图模型而不是Viewbag
public IActionResult Create(PerformanceManagementRecord performanceManagementProcess, int quarter)
{
var vm = new IndividualAction();
vm.Quarter = new IndividualAction.QuarterList();
vm.QuarterId = quarter;
vm.PerformanceManagementProcessId = performanceManagementProcess.Id;
vm.QuarterId = quarter;
PopulateStrategicActionDropDownList();
return View(vm);
}