[英]asp.net mvc jquery dropdown validation
how i can validate dropdownlist with unobtrusive javascript ? 我如何用不引人注目的javascript验证dropdownlist? As its validating textbox for required validator , but its not working for dropdownlist ?
作为验证所需验证器的文本框,但它不适用于下拉列表?
Need to change unobtrusive js file for it ? 需要为它改变不显眼的js文件吗? or is there any other option to validate dropdownlist ?
或者还有其他选项来验证下拉列表吗?
i want to show errors when i checked form.validate() in my javascript . 我想在我的javascript中检查form.validate()时显示错误。
$(function () {
$("#form1").validate({
rules: {
ProductCategoryList: {
required: true
}
},
messages: {
ProductCategoryList: {
required: "This field is required"
}
}
});
});
您必须保留第一个选项value=""
才能生效。
<asp:ListItem Value="">- Select Something -</asp:ListItem>
If you want to integrate the validation jQuery you need to specify the field 如果要集成验证jQuery,则需要指定字段
Use DropDownListFor
not DropDownList
使用
DropDownListFor
而不是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. 如果列表与字段具有相同的名称,则不需要将
ViewBag.Profile_Is as SelectList
因为如果在列表中发送空值,则它不适用于编辑方案。
The best I've seen is using the jQuery Form Validation plugin: 我见过的最好的是使用jQuery Form Validation插件:
http://plugins.jquery.com/project/validate http://plugins.jquery.com/project/validate
http://bassistance.de/jquery-plugins/jquery-plugin-validation/ http://bassistance.de/jquery-plugins/jquery-plugin-validation/
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"
}
}
});
});
I got this working by creating a property on the Viewmodel which is used for binding to the selecteditem in the DropDownList. 我通过在Viewmodel上创建一个属性来实现这个功能,该属性用于绑定DropDownList中的selecteditem。 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): 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;
}
Note : the SelectedPlantID does not have to be a field on your model. 注意:SelectedPlantID不必是模型上的字段。
Hope this works for you! 希望这对你有用!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.