ASP MVC 3:我怎样才能做一个选择列表客户端验证? [英] ASP MVC 3: How can I do Client side Validation on a Select List?

查看:134
本文介绍了ASP MVC 3:我怎样才能做一个选择列表客户端验证?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在这里读了几个问题/答案后,我设法解决如何选择列表添加到表单并用数据填充它,像这样:

After reading a few question/answers here I have managed to work out how to add a Select list to a form and fill it with data, like so:

@Html.DropDownList("S", new SelectList(ViewBag.S, "Id", "Nme"), "-- Sel a S --")

和它完美的作品。不过,我想补充一些的客户端验证的验证用户是否选择一个选项,而不是在默认离开它。

And it works perfectly. However I would like to add some client-side Validation To validate whether the user has selected an option and not left it at the Default.

我用自带的MVC 3,所以presumably我必须做​​一些与HTML.ValidationMessage,但什么标准的jQuery的东西吗?

I'm using the standard jquery stuff that comes with mvc 3, so presumably I have to do something with HTML.ValidationMessage, but what?

和不能为我的生活工作方式。

And Can't for the life of me work out how.

TIA。

好吧,我不得不通过其在JQuery的土地如何做到一看,通过添加htmlattribute像这样刚刚找到:

Ok I had a look through how its done in JQuery land and found just by adding an htmlattribute like so:

new {@class='required'}

我Html.DropDownList声明,并添加validationMessage,解决了这个问题对我来说。

to my Html.DropDownList statement, and adding validationMessage, fixes the problem for me.

推荐答案

如果您正在使用jQuery验证,那么你可以简单地添加发票一式的CSS类和有DropDownList中所要求的验证,所提供的默认值是空的。

If you are using the jquery validation then you may simply add the css class reuired and have the required validation for the dropdownlist, provided the default value is empty.

这篇关于ASP MVC 3:我怎样才能做一个选择列表客户端验证?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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