Asp.net mvc 4 客户端验证在模式对话框中不起作用

Asp.net mvc 4 客户端验证在模式对话框中不起作用,asp.net-mvc-4,Asp.net Mvc 4,我有一个模式弹出对话框,加载表单提交到数据库。如果填写了所有字段,则表单可以正常工作。如果我将字段留空并单击“创建”按钮,则验证应该启动,但不会启动。我得到以下错误: 一个或多个实体的验证失败。有关详细信息,请参阅“EntityValidationErrors”属性。 现在我知道错误是什么了。字段中的值为null,提交到数据库时不能为null。ModelState也是false。 这是我的模型: using System.ComponentModel.DataAnnotations; using

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

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; }
}
}
使用System.ComponentModel.DataAnnotations;
使用MVC_CSAlerts.Models;
名称空间MVC_CSAlerts.Models
{
使用制度;
使用System.Collections.Generic;
使用系统组件模型;
公共部分类csAlert
{
public int AlertID{get;set;}
[必需(AllowEmptyStrings=false,ErrorMessage=“必须输入路由”)]
公共字符串路由{get;set;}
[必需(AllowEmptyStrings=false,ErrorMessage=“必须输入问题”)]
公共字符串问题{get;set;}
公共字符串迂回{get;set;}
[显示名称(“日期”)]
[数据类型(数据类型.日期时间)]
[必需]
公共可为空的日期输入{get;set;}
[必需]
[显示名称(“输入人”)]
{get;set;}输入的公共字符串
公共可空计数{get;set;}
[显示名称(“发送电子邮件”)]
公共字符串sendmail{get;set;}
[DisplayName(“这是子警报吗”)]
公共字符串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")

 }
@model MVC\u csAlert.Models.csAlert
@{
ViewBag.Title=“创建”;
}
创建新警报
$(文档).ready(函数(){
//我们将测试数据采集器
$(“#DateEntered”).datepicker();
//我们将测试标签
$(函数(){
$(document.tooltip();
});
});
@使用(Ajax.BeginForm(“创建”、“警报”、新的AjaxOptions()))
{
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)
新警报
@LabelFor(model=>model.Routes)
@EditorFor(model=>model.Routes)
@Html.ValidationMessageFor(model=>model.Routes)
@LabelFor(model=>model.Issue)
@Html.TextAreaFor(model=>model.Issue)
@Html.ValidationMessageFor(model=>model.Issue)
@LabelFor(model=>model.Detour)
@Html.TextAreaFor(model=>model.Detour)
@Html.ValidationMessageFor(model=>model.Detour)
@LabelFor(model=>model.DateEntered)
@Html.ValidationMessageFor(Model=>Model.DateEntered)
@Html.JQueryUI().DatepickerFor(model=>model.DateEntered)
@Html.ValidationMessageFor(model=>model.DateEntered)
@LabelFor(model=>model.Count)
@EditorFor(model=>model.Count)
@Html.ValidationMessageFor(model=>model.Count)
@LabelFor(model=>model.sendmail)
@Html.DropDownListFor(model=>model.sendmail,新建)
选择列表(新建列表){
新的{value=“Y”,text=“Yes”},
新建{value=“N”,text=“No”}
},
“价值”,
“文本”,
“Y”))
@*@Html.EditorFor(model=>model.sendmail)*@
@Html.ValidationMessageFor(model=>model.sendmail)
@LabelFor(model=>model.IsChildAlert)

尝试将您的:
@Scripts.Render(“~/bundles/jqueryval”)
移动到
父视图中,而不是将其置于
模式视图中