客户端验证在Modal对话框中不起作用

时间:2013-09-04 19:17:32

标签: asp.net-mvc-4

我有一个模态弹出对话框,用于加载要提交给数据库的表单。如果填写了所有字段,表单工作正常。如果我留下空白字段并单击“创建”按钮,则验证应该启动但不会。我收到以下错误: 一个或多个实体的验证失败。有关详细信息,请参阅“EntityValidationErrors”属性。 现在我知道错误是什么。字段中的值为null,并且它们不能为空以提交到数据库。 ModelState也是假的。 这是我的模特:

using System.ComponentModel.DataAnnotations;
using MVC_CSAlerts.Models;


namespace MVC_CSAlerts.Models
 {
using System;
using System.Collections.Generic;
using System.ComponentModel;

public partial class csAlert
{
    public int AlertID { get; set; }

     [Required(AllowEmptyStrings = false, ErrorMessage = "Must Enter a Route")]
    public string Routes { get; set; }

    [Required(AllowEmptyStrings = false, ErrorMessage = "Must Enter an Issue")]
    public string Issue { get; set; }

    public string Detour { get; set; }

    [DisplayName("Date")]
    [DataType(DataType.DateTime)]
    [Required]
    public Nullable<System.DateTime> DateEntered { get; set; }

    [Required]
    [DisplayName("Entered By")]
    public string EnteredBy { get; set; }

    public Nullable<int> Count { get; set; }

    [DisplayName("Send Email")]
    public string SendEmail { get; set; }

    [DisplayName("Is this a child alert")]
    public string IsChildAlert { get; set; }
}
}

这是我的观点

  @model MVC_CSAlerts.Models.csAlert


  @{
ViewBag.Title = "Create";
 }
 <h2>Create New Alert</h2>

  <script type="text/javascript">
        $(document).ready(function () {
            // We will test DataPicker
            $('#DateEntered').datepicker();
            // We will test tabs
            $(function () {
                $(document).tooltip();
            });
        });

    </script>

  @using (Ajax.BeginForm("Create","Alerts",new AjaxOptions()))
  {
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)


<fieldset>
    <legend>New Alert</legend>


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

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

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

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

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

    <div class="editor-label">
        @Html.LabelFor(model => model.SendEmail)
    </div>
    <div class="editor-field">
                          @Html.DropDownListFor(model => model.SendEmail, new          


  SelectList(new List<object>{


 new{ value = "Y", text="Yes"},


  new{ value = "N",text="No"}
                                                                                 },


  "value",


  "text",
                                                                                 "Y"))


     @*                  @Html.EditorFor(model => model.SendEmail)*@
        @Html.ValidationMessageFor(model => model.SendEmail)
    </div>

    <div class="editor-label">
        @Html.LabelFor(model => model.IsChildAlert)
    </div>        <div class="editor-field>   @Html.DropDownListFor(model =>      



   model.IsChildAlert, new SelectList(new List<object>{


 new{ value = "Y", text="Yes"},


  new{ value = "N",text="No"}
                                                             },


 "value",


 "text",
                                                                                 "Y"))




     @*            @Html.EditorFor(model => model.IsChildAlert)*@
        @Html.ValidationMessageFor(model => model.IsChildAlert)
    </div>

    <p>
        <input type="submit" value="Create New Alert" />
    </p>
</fieldset>
  }


  @section Scripts {

  @Scripts.Render("~/bundles/jqueryval")

 }

如何加载客户端验证?我必须在模态窗口中进行javascript验证吗?

感谢

1 个答案:

答案 0 :(得分:0)

尝试将您的@Scripts.Render("~/bundles/jqueryval")移至Parent View,而不是将其移至Modal View