这是我的模特:
[Required]
[Display(Name = "I'm a:")]
public bool Sex { get; set; }
我的编辑模板:
@Html.LabelFor(model => model.RegisterModel.Sex)
@Html.EditorFor(model => model.RegisterModel.Sex)
但是,这将呈现如下:
Soy:
我如何为男性和女性渲染一些漂亮的单选按钮?我的模型必须具有什么数据类型?
编辑:
这是我新的更新代码:
//Model:
[Required]
[Display(Name = "Soy:")]
public Gender Sex { get; set; }
}
public enum Gender
{
Male = 1,
Female = 2
}
//Viewmodel:
Informacion Personal
@Html.LabelFor(model => model.RegisterModel.Nombre)
@Html.EditorFor(model => model.RegisterModel.Nombre)
@Html.LabelFor(model => model.RegisterModel.Apellido)
@Html.EditorFor(model => model.RegisterModel.Apellido)
@Html.LabelFor(model => model.RegisterModel.Sex)
@Html.EditorFor(model => model.RegisterModel.Sex)
@Html.LabelFor(model => model.RegisterModel.Carnet)
@Html.EditorFor(model => model.RegisterModel.Carnet)
//EditorTemplate:
@model GoldRemate.WebUI.Models.Gender
@{
ViewBag.Title = "Gender";
}
当我运行它时,我收到此错误:
The model item passed into the dictionary is null, but this dictionary
requires a non-null model item of type
‘GoldRemate.WebUI.Models.Gender’.
造成这种情况的原因是什么?如何在表单中显示我的枚举值?