ValidationMessage始终显示/摘要不 - ASP.NET MVC 3

时间:2011-02-01 17:30:24

标签: asp.net-mvc validation

问题是:

我有一个Ajax.BeginForm,它基本上由两个下拉列表和提交按钮组成。如果用户没有从两个下拉列表中选择一个选项,我想要启动MVC 3验证。

这是没有发生的。而是打开页面,其中ValidationMessages显示在ddls旁边,如果提交的数据无效,则不会显示验证摘要。

代码是:

using (Ajax.BeginForm(MVC.Admin.TutorEditor.AddTutorCourse(Model.TutorName, 0, 0),
                        new AjaxOptions
                        {
                            UpdateTargetId = "TutorCourses",
                            OnBegin = "isValidPleaseWait",
                            LoadingElementId = "PleaseWait"
                        },
                        new { name = "AddTutorCourseForm", id = "AddTutorCourseForm" }))
        {
            Html.ValidationSummary("Sorry, there was an error. Please check below and try again:");                
            <fieldset>
                <legend>Add Course</legend>
                <div class="editor-label">
                    @Html.LabelFor(model => model.AllCourses)
                    @Html.DropDownList("CourseId", Model.AllCourses, "-- Choose Course --", new { style = "width:180px;" })
                    @Html.ValidationMessage("CourseId", "*: Required")
                </div>
                <div class="editor-label">
                    @Html.LabelFor(model => model.TutorRoles)
                    @Html.DropDownList("TutorRoleId", Model.TutorRoles, "-- Choose Role --", new { style = "width:180px;" })
                    @Html.ValidationMessage("TutorRoleId", "*: Required")
                </div>
                <input type="submit" value="Add Course" />
            </fieldset>
        }

AddTutorCourse行动是:

[HttpPost]
        public virtual ActionResult AddTutorCourse(string username, int CourseId, int TutorRoleId)
        {
            service.SetTutor(username);
            if (CourseId == 0) ModelState.AddModelError("CourseId", "You did not choose a Course");
            if (TutorRoleId == 0) ModelState.AddModelError("TutorRoleId", "You did not choose a Tutor Role");
            if(ModelState.IsValid)
            {
                service.AddTutorToCourse(CourseId, TutorRoleId);
            }
            if (Request.IsAjaxRequest())
            {
                return PartialView(Views.TutorCoursesPartial, service.ViewModel);
            }
            else
            {
                return View(Views.Details, service.ViewModel);
            }            
        }

包含的脚本文件是:

    <script id="script0" src="/Scripts/MicrosoftAjax.js" type="text/javascript"></script>
<script id="script1" src="/Scripts/MicrosoftMvcAjax.js" type="text/javascript"></script>
<script id="script2" src="/Scripts/MicrosoftMvcValidation.js" type="text/javascript"></script>
    <script id="script3" src="/Scripts/jquery-1.4.1.min.js" type="text/javascript"></script>
<script id="script4" src="/jQueryUI/jquery-ui-1.8.2.custom.min.js" type="text/javascript"></script>
    <script id="script5" src="/jQueryPlugins/Cookies/jquery.cookie.js" type="text/javascript"></script>
    <script type="text/javascript" src="/Scripts/jquery.unobtrusive-ajax.min.js"></script>

我做错了什么!?

3 个答案:

答案 0 :(得分:11)

我也遇到了这个问题,最后我发现你需要在验证类中添加CSS样式。然后,当页面首次打开时,它不会显示它:

.field-validation-valid { display: none; }
.validation-summary-valid {  display: none; }

答案 1 :(得分:3)

您是否包含所需的javascript文件?你需要:

  • 的jquery-1.4.1.js
  • jquery.validate.js
  • jquery.validate.unobtrusive.js(如果使用不显眼的验证)

答案 2 :(得分:1)

如果你不需要整个jquery验证库,那么@ diana的答案是个不错的选择。或者,如果您无论如何都需要服务器验证,那么最好通过在您的视图中包含此功能来禁用客户端验证:

@{ Html.EnableClientValidation(false); }