【问题标题】:Why isn't this dropdown validating client side?为什么这个下拉列表不验证客户端?
【发布时间】:2017-05-16 14:59:15
【问题描述】:

在此示例中,我有一个汽车型号,我想通过下拉菜单指定汽车的“品牌”。需要“make”。

我在 web.config 中启用了客户端验证:

 <appSettings>
    <add key="ClientValidationEnabled" value="true" />
    <add key="UnobtrusiveJavaScriptEnabled" value="true" />
  </appSettings>

我已经按照汽车模型的要求设置了“make”属性:

 public class car
    {
        public int carId { get; set; }

        [Required]
        public int makeId { get; set; }

        virtual public make make { get; set; }

        [Required]
        public string carModel { get; set; }
    }

    public class make {

        public int makeId { get; set; }

        [Required]
        public string  makeName { get; set; }

    }

视图中引用了JQuery Validation,但只有在提交表单时才验证下拉列表?

@model validation.Models.car

@{
    ViewBag.Title = "Create";
}

<h2>Create</h2>


@using (Html.BeginForm())
{
    @Html.AntiForgeryToken()

    <div class="form-horizontal">
        <h4>car</h4>
        <hr />
        @Html.ValidationSummary(true, "", new { @class = "text-danger" })
        <div class="form-group">
            @Html.LabelFor(model => model.makeId, "makeId", htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.DropDownList("makeId", null, "--Select--", htmlAttributes: new { @class = "form-control" })
                @Html.ValidationMessageFor(model => model.makeId, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.carModel, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.carModel, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.carModel, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            <div class="col-md-offset-2 col-md-10">
                <input type="submit" value="Create" class="btn btn-default" />
            </div>
        </div>
    </div>
}

<div>
    @Html.ActionLink("Back to List", "Index")
</div>

@section Scripts {
    @Scripts.Render("~/bundles/jqueryval")
}

如果我关闭 UnobtrusiveJavaScript,那么“carmodel”或“make”都不会验证客户端,都只会验证服务器端。

 <add key="UnobtrusiveJavaScriptEnabled" value="false" />

【问题讨论】:

  • 并且验证正在对 carModel 文本框进行?
  • 是的,这会验证客户端。但是,如果未选择汽车制造商,则仅在发布表单并且模型无法验证时才会标记此错误。
  • 您的问题是您在值类型(int)上放置了[Required] 属性。这是没用的,因为值类型总是有一个值,并且不能为空。因此,始终满足所需的功能,因为默认值 0 是一个值。让你的模型属性可以为空来解决这个问题。验证 carMake 值的原因是字符串类型是引用类型,因此可以为空。

标签: asp.net .net asp.net-mvc validation


【解决方案1】:

我在这里找到了解决方案:how to validate dropdownlist using the DataAnnotation?

我使用了 DropDownListFor 助手而不是 DropDownList。现在下拉验证客户端。

@Html.DropDownListFor(model => model.makeId, ViewBag.makes as IEnumerable<SelectListItem>, "-- Select --", htmlAttributes: new { @class = "form-control" })

我不完全确定为什么这会解决问题,如果有人能解释一下吗?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-03-29
    • 2011-11-08
    • 2013-11-23
    • 1970-01-01
    • 1970-01-01
    • 2019-10-07
    • 2018-11-28
    • 1970-01-01
    相关资源
    最近更新 更多