当表单无效时,form.valid()返回true

时间:2014-02-10 09:17:50

标签: javascript asp.net-mvc validation

我有一个由列表框和单选按钮组成的表单。我通过发送帖子的ajax提交表单。我的控制器中的actionlink返回局部视图。如果表单无效,我不想清除列表框中的选定项目,但如果表单有效,则应取消选中所有内容。由于某种原因,form.valid()始终为true。错误消息显示完美,所以我认为验证没有任何问题。以下是我的代码的一些片段:

查看:表单

<div id="researchContainer">
@using (Html.BeginForm("ResearchCompetence", "Planning", FormMethod.Post, new { @id = "researchForm" }))
{
    @Html.HiddenFor(x => x.Input.PlanningId)
    @Html.ValidationSummary()

    <h1>@Html.Label("OverviewResearchCompetences", Labels.OverviewResearchCompetences)</h1>

    <table class="table-output">
        <thead>
            <tr>
                <td>@Html.Label("Name", Labels.Name)</td>
                <td>@Html.Label("Level", Labels.Level)</td>
                <td class="text-align-right"></td>
            </tr>
        </thead>
        <tbody>
            @if (Model.CurrentResearchCompetences.Count == 0)
            {
                <tr>
                    <td>@Html.Label("NoCurrentCompetencesRes", Labels.NoCurrentCompetencesRes)</td>
                </tr>
            }
            else
            {
                foreach (var item in Model.CurrentResearchCompetences)
                {
                    <tr>
                        <td>@item.Vtc</td>
                        @{
                            var scoreOutput = new ILVO.Services.EmployeeManagement.ScoreOutput(item.VtcLevel);
                        }
                        <td>
                            @scoreOutput.ToString()
                            @if (!scoreOutput.ToString().Equals("Gevorderd"))
                            {
                                <span class="arrow-up">
                                    @Html.ImageLink("IncreaseLevel", "Planning", new { id = @item.Id, planningId = Model.Input.PlanningId, actionMethod = "JobCompetence" },
                                        "/Content/arrow-icon.png", Labels.IncreaseLevel, "")
                                </span>
                            }
                            @if (!scoreOutput.ToString().Equals("Basis"))
                            {
                                @Html.ImageLink("DecreaseLevel", "Planning", new { id = @item.Id, planningId = Model.Input.PlanningId, actionMethod = "JobCompetence" },
                                    "/Content/arrow-icon.png", Labels.DecreaseLevel, "")
                            }
                        </td>
                        <td class="text-align-right deleteLink">
                            @Html.ImageLink("DeleteVtc", "Planning", new { id = @item.Id, planningId = @Model.Input.PlanningId, actionMethod = "JobCompetence" },
                                "/Content/delete-icon.png", Labels.Delete, "")
                        </td>
                    </tr>
                }
            }
        </tbody>
    </table>

    <br />
    <h1>@Html.Label("AddResearchCompetence", Labels.AddResearchCompetence)</h1>

    <table>
        <thead>
            <tr>
                <td>@Html.Label("Disciplines", Labels.Disciplines)</td>
                <td>@Html.Label("Organisms", Labels.Organisms) <a id="clear-organisme" class="button-clear-vtc">Clear</a></td>
                <td>@Html.Label("Techniques", Labels.Techniques) <a id="clear-technique" class="button-clear-vtc">Clear</a></td>
            </tr>
        </thead>
        <tbody>
            <tr>
                <td>
                    @Html.ListBoxFor(x => x.Input.SelectedDiscipline, Model.Disciplines, new { @class = "vtc-listbox-height", @size = 1 })
                </td>
                <td>
                    @Html.ListBoxFor(x => x.Input.SelectedOrganism, Model.Organisms, new { @class = "vtc-listbox-height" })

                </td>
                <td>
                    @Html.ListBoxFor(x => x.Input.SelectedTechnique, Model.Techniques, new { @class = "vtc-listbox-height" })

                </td>
            </tr>
        </tbody>
    </table>

    <div id="after-selection-ok">
        <h1>@Html.Label("ChooseLevel", Labels.ChooseLevel)</h1>
        @Html.RadioButtonFor(x => x.Input.Score, 0, new { @id = "radio1" }) @Html.Label("radio1", Labels.Basic, new { @class = "radio-label" })<br />
        @Html.RadioButtonFor(x => x.Input.Score, 1, new { @id = "radio2" }) @Html.Label("radio2", Labels.Intermediate, new { @class = "radio-label" })<br />
        @Html.RadioButtonFor(x => x.Input.Score, 2, new { @id = "radio3" }) @Html.Label("radio3", Labels.Expert, new { @class = "radio-label" })<br />
        <br />
        <input class="button" type="submit" name="AddResearchCompetence" value="@Labels.Submit" />
        @Html.ActionLink(Labels.GoBack, "Detail", new { id = @Model.Input.PlanningId }, new { @class = "button margin-left" })
    </div>
}

控制器:Actionlink

[HttpPost]
    public ActionResult ResearchCompetence(ResearchCompetenceInputModel input)
    {
         // do some validations

        // if validations are ok = add competence else return view

        return PartialView("ResearchCompetence", new ResearchCompetenceModel(researchCompetences, currentResearchCompetences, input.PlanningId));
    }

我的Ajax电话

$("#researchForm").submit(function () {
        $.ajax({
            type: 'POST',
            url: '@Url.Action("ResearchCompetence")',
            data: $("#researchForm").serialize(),
            success: function (data) {             
                $("#researchContainer").html(data);
                $("#researchForm").validate();

                if ($("#researchForm").valid()) { // always true
                    $("#Input_SelectedOrganism").val('');
                    $("#Input_SelectedTechnique").val('');
                    $("input[name='Input.Score']").attr('checked', false);
                }
            }
        });
        return false;
    });

我也加载了这些脚本:

@Scripts.Render("~/bundles/modernizr")
@Scripts.Render("~/Scripts/jquery-1.10.2.js")
@Scripts.Render("~/Scripts/jquery.validate.min.js")
@Scripts.Render("~/Scripts/jquery.validate.unobtrusive.js")
@Scripts.Render("~/Scripts/jquery-ui-1.10.3.js")
@Scripts.Render("~/Scripts/tinymce/tinymce.min.js")
@Scripts.Render("~/Scripts/ckeditor/ckeditor.js")
@Scripts.Render("~/Scripts/site.js")
@Scripts.Render("~/Scripts/jquery.ui.datepicker-nl-BE.js")

0 个答案:

没有答案