如何使用Html.EditorFor呈现在MVC3单选按钮? [英] How do I use Html.EditorFor to render radio buttons in MVC3?

查看:242
本文介绍了如何使用Html.EditorFor呈现在MVC3单选按钮?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是我的模型:

[Required]
[Display(Name = "I'm a:")]
public bool Sex { get; set; }

和我的编辑模板:

<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?

下面是我的新更新code:

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:

传递到字典的模型项是空的,但本词典
  需要类型的非空模型项目
  GoldRemate.WebUI.Models.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'.

是什么原因造成这一点,我怎么能显示我的形式我的枚举值?

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:

 public Gender Sex { get; set; }

然后在你看来,你这样做:

And then in your view you would do this:

 Html.EditorFor(x => x.RegisterModel.Sex);

然后,你会在这里有一个EditorTemplate:

Then you would have an EditorTemplate here:

 ~/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);
}

所以我在Visual Studio中模拟这一点,而这按预期工作。试试吧。

So I modeled this in Visual Studio, and this works as expected. Try it out.

这篇关于如何使用Html.EditorFor呈现在MVC3单选按钮?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆