0

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

谢谢

4

1 回答 1

0

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

于 2013-10-27T08:11:03.910 回答