MVC 5-添加客户端验证以拒绝默认值吗? [英] MVC 5 - Adding Client Validation to refuse the default value?

查看:62
本文介绍了MVC 5-添加客户端验证以拒绝默认值吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 DropDownListFor 并提供的默认选择-选择Make-,该值为空.我想向用户提供操作提示(因此选择选择"),但不想让他们实际提交该值.目前,该网站允许通过此操作.

I'm using a DropDownListFor and provide a default selection of -- Select Make -- which has an empty value. I want to provide the user with a hint of what to do (thus the 'Select Make') but don't want to allow them to actually submit that value. Currently the website allows this to go through.

我认为添加最小长度2可以阻止它,但是没有运气.

I thought that adding a minimum length of 2 would prevent it but no luck.

.NET MVC是我的新手,所以让我知道我是用完全错误的方式进行操作的.

I'm fairly new to .NET MVC so let me know if I'm doing this in the completely wrong way.

实际的POST请求正文为:

The actual POST request body is:

Make=&OtherArgument=1&NextArgument=test

查看代码:

@Html.LabelFor(m => m.Make)
@Html.DropDownListFor(m => m.Make, Model.Make, "-- Select Make --")
@Html.ValidationMessageFor(m => m.Make)

型号代码:

    [Required(ErrorMessage = "*", AllowEmptyStrings = false)]
    [StringLength(50, MinimumLength = 2, ErrorMessage = "*")]
    public IEnumerable<SelectListItem> Make = new List<SelectListItem>
    {
        new SelectListItem
        {
            Text = "Deere",
            Value = "Deere"
        },
        new SelectListItem
        {
            Text = "Case",
            Value = "Case"
        },
        new SelectListItem
        {
            Text = "CAT",
            Value = "CAT"
        }
    };

推荐答案

这里的第一个问题是 所选模型 用户可以选择的模型 .在模型中添加 ModelSelect 属性即可解决此问题(以及一些视图更改).

The first issue here is the distinction between the model selected and the models that a user can select. Adding the ModelSelect property to your model solves this (and some view changes).

第二个问题是通过添加脚本引用来启用jquery的非侵入式验证.

The second issue is enabling jquery unobtrusive validation by adding the script references.

这些是您需要进行的操作,以进行防止表单提交(假定为MVC4 +)的javascript验证.

These are the steps you need to take to get this working with javascript validation preventing form submission (assuming MVC4+).

模型

    public class AModel
    {
        [Required(ErrorMessage = "*", AllowEmptyStrings = false)]
        [StringLength(50, MinimumLength = 2, ErrorMessage = "*")]
        public string SelectedMake { get; set; }
        public IEnumerable<SelectListItem> Make = new List<SelectListItem>
        {
            new SelectListItem
            {
                Text = "Deere",
                Value = "Deere"
            },
            new SelectListItem
            {
                Text = "Case",
                Value = "Case"
            },
            new SelectListItem
            {
                Text = "CAT",
                Value = "CAT"
            }
            };
    }

请注意SelectedMake属性

查看

@using(Html.BeginForm()){
    @Html.LabelFor(m => m.Make)
    @Html.DropDownListFor(m => m.SelectedMake, Model.Make, "-- Select Make --")
    @Html.ValidationMessageFor(m => m.SelectedMake)
    <input type="submit" value="submit" />
}

注意Selected在下拉菜单的第一个参数中以及ValidationMessageFor

脚本参考

<script src="~/Scripts/jquery-1.8.2.min.js"></script>
<script src="~/Scripts/jquery.validate.min.js"></script>
<script src="~/Scripts/jquery.validate.unobtrusive.min.js"></script>

需要启用jquery验证

屏幕抓取

请注意红色星号,jquery不引人注目的现在可以阻止表单提交.

这篇关于MVC 5-添加客户端验证以拒绝默认值吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆