Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

asp.net mvc - jQuery Chosen Dropdown validation client site doesn't work

My model class contains of required lookup value which is a lookup-based record:

[Required]
[DisplayName("Business Unit")]
public string value { get; set; }

[Required] //not working on client side?
[DisplayName("Business Group")]
public int id_businessgroup { get; set; }

View:

    <div class="editor-label">
        @Html.LabelFor(model => model.value)
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.value)
        @Html.ValidationMessageFor(model => model.value)
    </div>

    <div class="editor-label">
        @Html.LabelFor(x=>x.BusinessGroup.value)
    </div>
    <div class="editor-field">
        @Html.DropDownListFor(x => x.id_businessgroup, new SelectList(ViewBag.BusinessGroups,"id","value"),"Please select group from list...")
        @Html.ValidationMessageFor(x => x.id_businessgroup)
    </div>

@section scripts{
  @Html.Partial("ScriptUseChosen")
}

Controller:

public ActionResult Create()
    {
        ViewBag.BusinessGroups = DB.BusinessGroups.Where(x => x.is_active).OrderBy(x => x.value).ToList();
        return View();
    }

Web.config:

<add key="ClientValidationEnabled" value="true" />
<add key="UnobtrusiveJavaScriptEnabled" value="true" />

BundleConfig.cs:

            bundles.Add(new ScriptBundle("~/bundles/jqueryval").Include(
            "~/Scripts/jquery.validate*",
            "~/Scripts/jquery.unobtrusive*"));

Validation for value is working perfectly on client side, but it's not working on the Chosen's dropdown. What could be the reason?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

jQuery Chosen updates the html by making the original select hidden (style="Display:none"). By default jquery.validate (1.9.0) ignores hidden elements. You can override the default using

$.validator.setDefaults({ 
  ignore: []
});

or

$('form').validate({
    ignore: []
});

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...