为GenderEnum类型单选按钮自定义所需的消息

时间:2013-02-19 15:43:43

标签: c# asp.net-mvc enums required-field

我正在尝试在客人未查看性别广播时显示此消息“O camposexoéobrigatório”,但我得到的只是显示:“模型字段是必需的。”

代码在这里:

// GenderEnum.cs
namespace Diorama.Site.Models
{
    public enum GenderEnum
    {
        Male,
        Female
    }
}

// RegisterViewModel.cs
[Required(ErrorMessage = "O campo sexo é obrigatório")]
[Name("Moça ou rapaz?")]
public GenderEnum Gender { get; set; }

public static void Map()
{
    Mapper.CreateMap<RegisterViewModel, User>()
        .ForMember(u => u.Gender,
                    opt => opt.MapFrom(r => r.Gender.ToString()[0]));
    Mapper.CreateMap<User, RegisterViewModel>()
        .ForMember(r => r.Gender,
                    o => o.MapFrom(u =>
                                    u.Gender.ToLower() == "M"
                                        ? GenderEnum.Male
                                        : GenderEnum.Female));
}

//_RegisterForm.cshtml
<div class="controls">
@Html.EditorFor(model => model.Gender)
@Html.ValidationMessageFor(model => model.Gender, ""
                                    , new { @class = "help-block" })
</div>

0 个答案:

没有答案