首页 > 解决方案 > 未调用自定义编辑器模板

问题描述

我有一个使用数字 EditorTemplates 并针对 .NET Framework 4.6.1 的 MVC 应用程序。我有一个我想用来将枚举呈现为一组单选按钮,我的用户可以从中选择。

在我的 ViewModel 中,我有以下内容:

public enum ApplicationType { Register, Replace, Renew }

[UIHint("EnumRadioButtons")]
public ApplicationType AppType { get; set; }

在我看来,我正在使用

@Html.EditorForModel(Model)

我的编辑器模板看起来像:

@model Enum

var listItems = Enum.GetValues(Model.GetType()).OfType<Enum>().Select( e => 
  new SelectListIten()
  {
    Text = getDescription(e), //Function just used to get the Display Value
    Value = e.ToString(),
    Selected = e.Equals(Model)
  });
foreach (var li in listItems)
{
  //Render the Radio Button
}

我还有另一个枚举编辑器模板,它将我的枚举呈现到下拉列表中。在为我的模型渲染 EditorModel 时,使用标准的 Enum 编辑器模板,而不是我的自定义 EnumRadioButtons 模板。有任何想法吗?

我也用过[DataType("EnumRadioButtons")]装饰无济于事。

这在我使用这种确切的方法和特定的编辑器模板但针对 .NET Framework 4.0 之前有效。

谢谢

西蒙

标签: c#asp.net-mvcmodel-view-controllermvc-editor-templates

解决方案


推荐阅读