asp.net mvc jquery 下拉验证

发布于 2024-10-09 01:38:33 字数 154 浏览 3 评论 0原文

我如何使用不显眼的 javascript 验证下拉列表?作为所需验证器的验证文本框,但它不适用于下拉列表?

需要更改不显眼的 js 文件吗?或者还有其他选项来验证下拉列表吗?

我想在我的 javascript 中检查 form.validate() 时显示错误。

how i can validate dropdownlist with unobtrusive javascript ? As its validating textbox for required validator , but its not working for dropdownlist ?

Need to change unobtrusive js file for it ? or is there any other option to validate dropdownlist ?

i want to show errors when i checked form.validate() in my javascript .

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

死开点丶别碍眼 2024-10-16 01:38:33

标记

 <p>
        <label for="ProductCategoryList">Product Categories</label>
        <%= Html.DropDownList("ProductCategoryList", ViewData["ProductCategories"] as SelectList)%>
    </p>

和验证脚本

        $.validator.addMethod("selectNone",
            function (value, element) {
                return this.optional(element) || element.selectedIndex != 0;
            },
           "Please select an option."
        );


        $(function () {
            $("#form1").validate({
                rules: {
                    ProductCategoryList: {
                        selectNone: true
                    }

                },
                messages: {
                    ProductCategoryList: {
                        selectNone: "This field is required"
                    }
                }
            });
        });

Markup

 <p>
        <label for="ProductCategoryList">Product Categories</label>
        <%= Html.DropDownList("ProductCategoryList", ViewData["ProductCategories"] as SelectList)%>
    </p>

And the validation script

        $.validator.addMethod("selectNone",
            function (value, element) {
                return this.optional(element) || element.selectedIndex != 0;
            },
           "Please select an option."
        );


        $(function () {
            $("#form1").validate({
                rules: {
                    ProductCategoryList: {
                        selectNone: true
                    }

                },
                messages: {
                    ProductCategoryList: {
                        selectNone: "This field is required"
                    }
                }
            });
        });
非要怀念 2024-10-16 01:38:33
    $(function () {
        $("#form1").validate({
            rules: {
                ProductCategoryList: {
                    required: true
                }

            },
            messages: {
                ProductCategoryList: {
                    required: "This field is required"
                }
            }
        });
    });
    $(function () {
        $("#form1").validate({
            rules: {
                ProductCategoryList: {
                    required: true
                }

            },
            messages: {
                ProductCategoryList: {
                    required: "This field is required"
                }
            }
        });
    });
情何以堪。 2024-10-16 01:38:33

您必须保留第一个选项 value="" 才能正常工作。

<asp:ListItem Value="">- Select Something -</asp:ListItem>

You have to leave the first option value="" for it to work.

<asp:ListItem Value="">- Select Something -</asp:ListItem>
撩发小公举 2024-10-16 01:38:33

我通过在 Viewmodel 上创建一个属性来实现此目的,该属性用于绑定到 DropDownList 中的选定项目。通过一个示例可以清楚地说明:

视图中的代码:

        <div class="editor-field">
           @Html.DropDownListFor(model => model.SelectedPlantID,
                                 new SelectList(Model.Plants, "Value", "Text"),
                                 " ", new { id = "ddlPlant" })

          @Html.ValidationMessageFor(model => model.SelectedPlantID)
    </div>

ViewModel 中的代码将是(强类型化到视图):

            private List<SelectListItem> _plants = new List<SelectListItem>();
    [Required]
    [Display(Name = "Plant")]
    public List<SelectListItem> Plants
    {
        get
        {
            return (_plants);
        }

        set
        {
            _plants = value;
        }
    }

    public Guid SelectedPlantID
    {
        get;
        set;
    }

注意:SelectedPlantID 不必是模型上的字段。

希望这对你有用!

I got this working by creating a property on the Viewmodel which is used for binding to the selecteditem in the DropDownList. An example will make it clear:

The code in the View:

        <div class="editor-field">
           @Html.DropDownListFor(model => model.SelectedPlantID,
                                 new SelectList(Model.Plants, "Value", "Text"),
                                 " ", new { id = "ddlPlant" })

          @Html.ValidationMessageFor(model => model.SelectedPlantID)
    </div>

The code in the ViewModel will be(strongly typed to the view):

            private List<SelectListItem> _plants = new List<SelectListItem>();
    [Required]
    [Display(Name = "Plant")]
    public List<SelectListItem> Plants
    {
        get
        {
            return (_plants);
        }

        set
        {
            _plants = value;
        }
    }

    public Guid SelectedPlantID
    {
        get;
        set;
    }

Note : the SelectedPlantID does not have to be a field on your model.

Hope this works for you!

江南月 2024-10-16 01:38:33

如果要集成验证 jQuery,则需要指定字段

使用 DropDownListFor 而不是 DropDownList

@Html.DropDownListFor(c => c.Profile_Id, null, "-- Choose --", new { @class = "input-large" })

如果您的列表与字段具有相同的名称,则无需输入ViewBag.Profile_Is as SelectList 因为如果您在列表中发送空值,它不适用于编辑场景。

If you want to integrate the validation jQuery you need to specify the field

Use DropDownListFor not DropDownList

@Html.DropDownListFor(c => c.Profile_Id, null, "-- Choose --", new { @class = "input-large" })

If your list has the same name as the field, you don't need to put ViewBag.Profile_Is as SelectList because it doesn't work for the edit scenario if you send a null value in list.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文