Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
898 views
in Technique[技术] by (71.8m points)

asp.net mvc - RadioButtonFor not selecting value in editor templates

I have the following model:

public class Person
{
    public string Name { get; set; }
    public string Gender { get; set; }
}

I want the default gender to be female, so I set that up in the action:

public ActionResult Create()
{
    var model = new Person { Gender = "F" }; // default is female
    return View(model);
}

Finally, the view renders everything like this:

@model Person

@using (Html.BeginForm())
{
    @Html.EditorForModel()
    <p><input type="submit" value="Create" /></p>
}

This all works as expected. Now, let's say that instead of a simple textbox, I want to display the gender as a more intuitive pair of radio buttons. So I make the following template and save it to Shared/EditorTemplates/Gender.cshtml:

@model string
@Html.RadioButtonFor(model => model, "F") Female
@Html.RadioButtonFor(model => model, "M") Male

Finally I decorate Gender with [UIHint("Gender")].

Gender is now properly rendered with radio buttons, which is great, BUT...

The problem

Female no longer comes pre selected as the default value and instead I end up with the two radio buttons blank. Am I missing something?

Interestingly, if I move RadioButtonFor from the template to the view (changing model => model to model => model.Gender), then everything works as expected. I know this is a viable workaround, but these templated helpers are such an amazing, addictive convenience that I would rather exhaust all possibilities before letting them go.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I know it's ugly but the following might work:

@model string
@Html.RadioButton("", "F", Model == "F") Female
@Html.RadioButton("", "M", Model == "M") Male

The problem with the RadioButton helper is that if the first argument is null or empty it will always consider it as non checked thus making this helper inappropriate for editor templates.

Here's an excerpt from the MVC source code which illustrates what I mean:

bool isChecked = !string.IsNullOrEmpty(name) && string.Equals(htmlHelper.EvalString(name), b, StringComparison.OrdinalIgnoreCase);

As an alternative you could use a custom HTML helper to generate a list of radio buttons.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...