Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I use Html.EditorFor to render radio buttons in MVC3?

Here's my 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?


Edit:

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'.

What is causing this and how can I show the values of my enum in my form?

like image 460
Only Bolivian Here Avatar asked Aug 17 '11 19:08

Only Bolivian Here


1 Answers

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);

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

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

like image 129
Tejs Avatar answered Oct 25 '22 07:10

Tejs