首页 > 解决方案 > 无法在 MVC.NET Core 应用程序的单选按钮字段上看到必需消息

问题描述

我有一个单选按钮字段,我尝试将这个字段设为必填。但是,无论我在单击提交按钮后尝试什么,页面上都不会显示必填字段消息

这是单选按钮字段

                        <div class=" form-group">
                            <div class="bisformdynamiclabel"></div>
                            <br />
                            @Html.RadioButtonFor(model => model.BIS232Request.JSONData.OwnershipActivity.Ownership, "Yes", new { id = "OwnershipAnswer_true", onclick = "displayOwnershipFieldsRow(true)" })
                            <label for="OwnershipAnswer_true">Yes</label>
                            @Html.RadioButtonFor(model => model.BIS232Request.JSONData.OwnershipActivity.Ownership, "No", new { id = "OwnershipAnswer_false", onclick = "displayOwnershipFieldsRow(false)" })
                            <label for="OwnershipAnswer_false">No</label>
                        </div>

这是模型中的代码

    [Required(ErrorMessage = "Please select Yes or No for the Ownership")]
    public string Ownership { get; set; }

其他字段经过验证,没有任何问题。有人可以告诉我我做错了什么吗?

标签: asp.net-core-mvc

解决方案


您没有任何实际显示消息的东西。具体来说,您需要以下内容:

@Html.ValidationMessageFor(m => m.BIS232Request.JSONData.OwnershipActivity.Ownership)

或者使用标签助手:

<span asp-validation-for="BIS232Request.JSONData.OwnershipActivity.Ownership"></span>

推荐阅读