在 ASP.NET Core MVC 中使用 taghelpers 输入单选按钮

Input radio buttons in ASP.NET Core MVC with taghelpers

我正在尝试创建一个带有两个值的单选按钮的表单:自动和手动。

为了这样做,我将 an answer to a GitHub issue 修改为我的代码,但不幸的是,我在视图的 foreach 行中遇到一个问题,其中 "Model.GearingType" 无法识别,如果我将其更改为 "GearingType" 也无法识别。

谢谢!

ViewModel

public class EvaluationForm
{
   public enum GearingType
   {
       Manual,
       Automatic
   }

   [Required(ErrorMessage = "Please select your car gearing's type")]
   [Display(Name = "Gearing Type")]
   public GearingType SelectedGearingType { get; set; }

查看

<div class="row">
    <div class="col-md">
        @{
            foreach (Model.GearingType gearType in Enum.GetValues(typeof(Model.GearingType))
            {
                <label>
                    <input asp-for="SelectedGearingType" type="radio" value="@gearType" />
                    @gearType
                </label>
            }

        }
    </div>
</div>

您需要指定Type,而不是typeof语句中的模型实例

foreach (var gearType in Enum.GetValues(typeof(EvaluationForm.GearingType)))
{
    ....
}