mvc中 html是否单选按钮,c# – 如何使用Html.EditorFor在MVC3中渲染单选按钮?

这是我的模特:

[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’.

造成这种情况的原因是什么?如何在表单中显示我的枚举值?

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值