asp.net mvc 下拉列表的客户端验证?

发布于 2024-09-28 14:21:02 字数 938 浏览 1 评论 0原文

我只是想知道如何在 asp.net mvc 2 中启用下拉菜单的客户端验证。 这种情况是下拉列表将包含一个“选择”项目和其他项目的列表...,用户应该选择其他项目...当用户没有选择

public class FacilityBulletinModel
    {
        [DisplayName("Select a Facility")]
        public List<SelectListItem> ListFacility { get; set; }

        [DisplayName("Facility Bulletin")]
        [Required(ErrorMessage = "Please create a Bulletin")]
        public string FacilityBulletin { get; set; }

        [DisplayName("Active")]
        public bool Active { get; set; }

       [HiddenInput(DisplayValue = false)]
        public int SiteId { get;set;}
    }

我视图中的其他项目时,应该触发验证

 Select Facility <span class="err">*</span><br />
    <%=Html.DropDownListFor(model => model.ListFacility, null, new {onChange="updateSiteId()" })%>
   <span class="err"> <%= Html.ValidationMessageFor(model => model.ListFacility) %></span>

i just wanted to know how to enable client side validations for dropdowns in asp.net mvc 2.
The scenario would be that the dropdown will contain a "Select" item and the list of other items..,The user should select other items... the validation should fire when the user does not select the other items

public class FacilityBulletinModel
    {
        [DisplayName("Select a Facility")]
        public List<SelectListItem> ListFacility { get; set; }

        [DisplayName("Facility Bulletin")]
        [Required(ErrorMessage = "Please create a Bulletin")]
        public string FacilityBulletin { get; set; }

        [DisplayName("Active")]
        public bool Active { get; set; }

       [HiddenInput(DisplayValue = false)]
        public int SiteId { get;set;}
    }

in my view

 Select Facility <span class="err">*</span><br />
    <%=Html.DropDownListFor(model => model.ListFacility, null, new {onChange="updateSiteId()" })%>
   <span class="err"> <%= Html.ValidationMessageFor(model => model.ListFacility) %></span>

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

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

发布评论

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

评论(1

淡淡離愁欲言轉身 2024-10-05 14:21:02

首先,如果需要下拉列表,请将 [Required] 属性添加到模型属性中。

然后,在视图顶部的某处启用客户端验证:

<% Html.EnableClientValidation() %>

然后只需添加一条验证消息:(

<div class="inputField">
    <%= Html.LabelFor(model => model.property)%>
    <%= Html.DropDownListFor(model => model.property, (SelectList)ViewData["myselelectlist"])%>
    <%= Html.ValidationMessageFor(model => model.property)%>
</div>

这需要加载 MicrosoftMvcValidation.js)

First, if a dropdown is required, add the [Required] attribute to your model property.

Then, enable client side validation somewhere at the top of your view:

<% Html.EnableClientValidation() %>

Then just add a validation message:

<div class="inputField">
    <%= Html.LabelFor(model => model.property)%>
    <%= Html.DropDownListFor(model => model.property, (SelectList)ViewData["myselelectlist"])%>
    <%= Html.ValidationMessageFor(model => model.property)%>
</div>

(this requries MicrosoftMvcValidation.js to be loaded)

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