0

I have a few remote validations to make for client side validation. Two of them are almost identical, but one works and the other doesn't, the break point never gets hit.

Working One:

View:

@Html.EditorFor(model => model.ClassNumber, new { @id = "ClassNumber" })
@Html.ValidationMessageFor(model => model.ClassNumber)

Model:

[EdmScalarPropertyAttribute(EntityKeyProperty = false, IsNullable = false)]
[Required(ErrorMessage = "Required")]
[Remote("CheckClassNumber", "Course", AdditionalFields = "Title", HttpMethod = "POST",
    ErrorMessage = "Already Exists")]
[DataMemberAttribute()]
public global::System.Int32 ClassNumber
{

Method:

[HttpPost]
public JsonResult CheckClassNumber(int ClassNumber, int Title)
{
    var course = db.Courses.Any(c => c.ClassNumber.Equals(ClassNumber) && c.Title.Equals(Title));

    return Json(!course);
}

Source Code:

    <select class="chzn-select" data-placeholder=" -- Select Course -- " data-val="true" data-val-number="The field Title must be a number." data-val-required="Required" id="Title" name="Title">
        <option value=""></option>
        <option value="1">Option 1</option>
        <option value="2">Option 2</option>
        <option value="3">Option 3</option>
    </select>
    <span class="field-validation-valid" data-valmsg-for="Title" data-valmsg-replace="true"></span>

Not Working One

View:

@Html.DropDownListFor(model => model.Student,
    @ViewBag.StudentID as SelectList, "",
    new { @class = "chzn-select", data_placeholder = " -- Select Student -- " })
@Html.ValidationMessageFor(model => model.Student)

Model:

[EdmScalarPropertyAttribute(EntityKeyProperty = false, IsNullable = false)]
[Required(ErrorMessage = "Required")]
[Remote("CheckIfEnrolled", "Enrollment", AdditionalFields = "Course", HttpMethod = "POST",
    ErrorMessage = "Already Enrolled")]
[DataMemberAttribute()]
public global::System.Int32 Student
{

Method:

[HttpPost]
public JsonResult CheckIfEnrolled(int Student, int Course)
{
    var exists = db.Enrollments.Any(e => e.Course.Equals(Course) && e.Student.Equals(Student));

    return Json(!exists);
}

Source Code:

<select class="chzn-select" data-placeholder=" -- Select Student -- " data-val="true" data-val-number="The field Student must be a number." data-val-remote="Already Enrolled" data-val-remote-additionalfields="*.Student,*.Course" data-val-remote-url="/QIEducation/Enrollment/CheckIfEnrolled" data-val-required="Required" id="Student" name="Student">
    <option value=""></option>
    <option value="8">Option 1</option>
    <option value="10">Option 2</option>
    <option value="11">Option 3</option>
</select>
<span class="field-validation-valid" data-valmsg-for="Student" data-valmsg-replace="true"></span>

I have made sure that the scripts are loaded in the correct order. The additional fields are typed correctly. There are no errors coming up in the debug or looking at the source code. I have looked around and the solutions for similar problems are already implemented.

4

1 回答 1

0

Alright. Turns out that the chosen plugin turns the select elements into a hidden element. Meaning that validation won't check it by default. I found a few solutions that worked for other people, but not for me. I ended up forcing a validation on change.

<script type="text/javascript">
    //Have to force a validation on change.
    $('#Student').on('change', function () {
        $('#Student').valid();
    });
</script>

Works every time and doesn't change the defaults in the jquery file.

于 2015-08-17T20:22:06.557 回答