[英]How do I use Html.EditorFor to render radio buttons in MVC3?
Here's my model:这是我的 model:
[Required]
[Display(Name = "I'm a:")]
public bool Sex { get; set; }
And my editor template:还有我的编辑器模板:
<div>
@Html.LabelFor(model => model.RegisterModel.Sex)
@Html.EditorFor(model => model.RegisterModel.Sex)
</div>
However this render to the following:但是,这呈现为以下内容:
<div>
<label for="RegisterModel_Sex">Soy:</label>
<input class="check-box" data-val="true" data-val-required="The Soy: field is required." id="RegisterModel_Sex" name="RegisterModel.Sex" type="checkbox" value="true" /><input name="RegisterModel.Sex" type="hidden" value="false" />
</div>
How would I render some nice radio buttons for Male and Female?我将如何为男性和女性呈现一些不错的单选按钮? What datatype would my model have to have?我的 model 必须有什么数据类型?
Here's my new updated code:这是我的新更新代码:
//Model:
[Required]
[Display(Name = "Soy:")]
public Gender Sex { get; set; }
}
public enum Gender
{
Male = 1,
Female = 2
}
//Viewmodel:
<fieldset>
<legend>Informacion Personal</legend>
<div>
@Html.LabelFor(model => model.RegisterModel.Nombre)
@Html.EditorFor(model => model.RegisterModel.Nombre)
</div>
<div>
@Html.LabelFor(model => model.RegisterModel.Apellido)
@Html.EditorFor(model => model.RegisterModel.Apellido)
</div>
<div>
@Html.LabelFor(model => model.RegisterModel.Sex)
@Html.EditorFor(model => model.RegisterModel.Sex)
</div>
<div>
@Html.LabelFor(model => model.RegisterModel.Carnet)
@Html.EditorFor(model => model.RegisterModel.Carnet)
</div>
</fieldset>
//EditorTemplate:
@model GoldRemate.WebUI.Models.Gender
@{
ViewBag.Title = "Gender";
}
<input type="radio" name="Sex" value="@Model" />
When I run this, I get this error:当我运行这个时,我得到这个错误:
The model item passed into the dictionary is null, but this dictionary requires a non-null model item of type 'GoldRemate.WebUI.Models.Gender'.传入字典的 model 项是 null,但此字典需要“GoldRemate.WebUI.Models.Gender”类型的非空 model 项。
What is causing this and how can I show the values of my enum in my form?是什么导致了这种情况,我怎样才能在我的表单中显示我的枚举值?
Create an enum like so:像这样创建一个枚举:
public enum Gender
{
Male = 1,
Female = 2
}
I'd alter your model slightly like so:我会稍微改变你的 model ,如下所示:
public Gender Sex { get; set; }
And then in your view you would do this:然后在您看来,您会这样做:
Html.EditorFor(x => x.RegisterModel.Sex);
Then you would have an EditorTemplate here:然后你会在这里有一个 EditorTemplate:
~/Views/Shared/EditorTemplates/Gender.cshtml
Which would have content like so:内容如下:
@model EditorTemplate.Models.Gender // switch to your namespace
@Html.LabelFor(x => x, "Male")
@if(Model == EditorTemplate.Models.Gender.Male)
{
@Html.RadioButtonFor(x => x, (int)EditorTemplate.Models.Gender.Male, new { @checked = "checked" });
}
else
{
@Html.RadioButtonFor(x => x, (int)EditorTemplate.Models.Gender.Male);
}
@Html.LabelFor(x => x, "Female")
@if(Model == EditorTemplate.Models.Gender.Female)
{
@Html.RadioButtonFor(x => x, (int)EditorTemplate.Models.Gender.Female, new { @checked = "checked" });
}
else
{
@Html.RadioButtonFor(x => x, (int)EditorTemplate.Models.Gender.Female);
}
So I modeled this in Visual Studio, and this works as expected.所以我在 Visual Studio 中对此进行了建模,并且按预期工作。 Try it out.试试看。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.