简体   繁体   English

使用可为空的枚举属性和带有空字符串值的附加选项呈现单选按钮

[英]Render a radiobuttonfor with a nullable enum property and addtional option with an empty string value

I am trying to display radio buttons on page where one of the radio buttons has to be selected otherwise an error should be displayed.我试图在必须选择其中一个单选按钮的页面上显示单选按钮,否则应显示错误。 The three options available are:可用的三个选项是:

  • Manual手动的
  • Automatic自动的
  • Unsure不确定

The option "Unsure" does not make sense to add to the GearboxTransmission enum as it is merely indicating that the user does not know what option they want to select at this moment in time.将选项“不确定”添加到 GearboxTransmission 枚举中没有意义,因为它只是表明用户不知道此时他们想要选择哪个选项。

I have the following enum:我有以下枚举:

public enum GearboxTransmission
{
    Automatic,
    SemiAutomatic,
    Manual
}

Model:模型:

public class BookingsIndexViewModel
{
    [Required(ErrorMessage = "Please select the preferred gearbox transmission to learn with.")]
    public GearboxTransmission? GearboxTransmission { get; set; }
}

And HTML:和 HTML:

<div class="form-group">
    <fieldset>
        <legend>Gearbox transmission</legend>
        <div class="radio">
            <label>
                @Html.RadioButtonFor(m => m.GearboxTransmission, GearboxTransmission.Manual) Manual
            </label>
        </div>
        <div class="radio">
            <label>
                @Html.RadioButtonFor(m => m.GearboxTransmission, GearboxTransmission.Automatic) Automatic
            </label>
        </div>
        <div class="radio">
            <label>
                @Html.RadioButtonFor(m => m.GearboxTransmission, string.Empty) Unsure
            </label>
        </div>
        @Html.ValidationMessageFor(m => m.GearboxTransmission, null, new { @class = "text-danger" })
    </fieldset>
</div>

As I have set the GearboxTransmission property to nullable I would expect that by creating a radio button with the value of an empty string when the page is posted back the GearboxTransmission property would be null, indicating that the option "Unsure" was selected.由于我已将 GearboxTransmission 属性设置为可为空,因此我希望通过在回发页面时创建一个值为空字符串的单选按钮,GearboxTransmission 属性将为空,表明选择了“不确定”选项。

What actually happens is the model view state is invalid and the validation message "Please select the preferred gearbox transmission to learn with."实际发生的是模型视图状态无效并且验证消息“请选择要学习的首选变速箱传输”。 appears due to the required attribute on the GearboxTransmission property.由于 GearboxTransmission 属性上的 required 属性而出现。

I would like it so that you have to specify an option, and if you don't you get the error "Please select the preferred gearbox transmission to learn with."我希望您必须指定一个选项,否则您会收到错误消息“请选择要学习的首选变速箱”。 unless you select the "Unsure" radio button in which case the form is submitted.除非您选择“不确定”单选按钮,在这种情况下提交表单。

Your code doesn't seem to be wrong as such, although I can't really tell without running it.您的代码似乎没有错,尽管我不运行它就无法判断。 Also, you're also aware of what's wrong with this - your validation 'requires' that there is a selection made.此外,您也知道这有什么问题 - 您的验证“要求”做出选择。

'Required' doesn't really check that a radio button is clicked, rather it checks whether the user selected a value. 'Required' 并没有真正检查单选按钮是否被点击,而是检查用户是否选择了一个值。

Clearly, you can't use validation to display an error message based on not selecting a value, when the value of one of your options is empty (you follow?).显然,当您的选项之一的值为空时(您遵循?),您不能使用验证来显示基于未选择值的错误消息。

There are 2 answers to this:对此有2个答案:

A) Forget about the error message and default the radios to 'unsure' although that could well lower the quality of the data capture. A) 忘记错误消息并将无线电默认为“不确定”,尽管这可能会降低数据捕获的质量。 B) Use an actual value for 'unsure', Add it to your GearboxTransmission Enum and flow it through Model and View etc. This would allow your empty selection to work. B) 使用“不确定”的实际值,将其添加到您的 GearboxTransmission Enum 并通过模型和视图等流动它。这将使您的空选择起作用。

If you really have to end up with an Enum value without 'Unsure', use a second one for model binding, but copy the Model Enum to the DB Enum in the controller.如果您真的必须得到一个没有“不确定”的枚举值,请使用第二个进行模型绑定,但将模型枚举复制到控制器中的数据库枚举。

Hope this helps.希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM