Asp.net mvc 4 使用数据注释在MVC4中验证不起作用

Asp.net mvc 4 使用数据注释在MVC4中验证不起作用,asp.net-mvc-4,data-annotations,Asp.net Mvc 4,Data Annotations,我在模型中使用了mvc验证所需的所有名称空间。 我的模型和视图代码如下,我已经尝试了所有的可能性,包括了所有的javascript using System; using System.Collections.Generic; using System.ComponentModel.DataAnnotations; using System.Linq; using System.Web; using System.Web.Mvc; using System.ComponentModel; na

我在模型中使用了mvc验证所需的所有名称空间。 我的模型和视图代码如下,我已经尝试了所有的可能性,包括了所有的javascript

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.ComponentModel;

namespace EAssessment.Models
{
    public class Question_Bank
    {
        [Display(Name="course")]
        [Required]
        public int Course_Id { get; set; }
        [Required]
        public int Standard_Id { get; set; }
        [Required]
        public int Subject_Id { get; set; }
        [Required]
        public int Lesson_Id { get; set; }
        [Required]
        public int Question_Type_Id { get; set; }
        [Required(ErrorMessage = "Required field")]
        public string Course_Name { get; set; }
        [Required(ErrorMessage = "Required field")]
        public string Standard_Name { get; set; }
        [Required(ErrorMessage = "Required field")]
        public string Subject_Name { get; set; }
        [Required(ErrorMessage = "Required field")]
        public string Lesson_Name { get; set; }
        [Required(ErrorMessage = "Required field")]
        public string Question_Type { get; set; }          

        public int Question_Id { get; set; }
        [Display(Name="course")]
        [Required()]
        public string Question{ get; set; }
        public int CntDynamicTextbox { get; set; }    

    }
}


<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script> 
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script> 
使用系统;
使用System.Collections.Generic;
使用System.ComponentModel.DataAnnotations;
使用System.Linq;
使用System.Web;
使用System.Web.Mvc;
使用系统组件模型;
名称空间EAssessment.Models
{
公共课问题库
{
[显示(Name=“课程”)]
[必需]
public int Course_Id{get;set;}
[必需]
公共int标准_Id{get;set;}
[必需]
public int Subject_Id{get;set;}
[必需]
公共int课程_Id{get;set;}
[必需]
公共整数问题类型Id{get;set;}
[必需(ErrorMessage=“必需字段”)]
公共字符串课程名称{get;set;}
[必需(ErrorMessage=“必需字段”)]
公共字符串标准_名称{get;set;}
[必需(ErrorMessage=“必需字段”)]
公共字符串Subject_Name{get;set;}
[必需(ErrorMessage=“必需字段”)]
公共字符串名称{get;set;}
[必需(ErrorMessage=“必需字段”)]
公共字符串问题类型{get;set;}
公共整数问题_Id{get;set;}
[显示(Name=“课程”)]
[必需()]
公共字符串问题{get;set;}
公共int-CntDynamicTextbox{get;set;}
}
}
我的Razor视图标记

@using (Html.BeginForm())
{
     @Html.AntiForgeryToken()    
     @Html.ValidationSummary(true)
   <div style="height: auto; border: 1px solid #DBDBDB; width: 900px;">


        <br />
        <span>
            <label class="label"style="margin-left: 100px;">
                Course:
            </label >
        </span>
        <span>@Html.DropDownListFor(Model => Model.Course_Id, Courses, "Select Course", new { id = "ddlCourse", style = "margin-left:17px;height:25px;width:230px;", onchange = "loadStandard(this)" })</span>
           @Html.ValidationMessageFor(model => model.Course_Id)
      <span>
            <label  class="label" style="margin-left: 100px;">
                Standard:
            </label>
        </span>
        <span>@Html.DropDownListFor(Model => Model.Standard_Id, Standard, "Select Standard", new { id = "ddlStandard", style = "margin:0px 10px 0px 10px;height:25px;width:230px;", onchange = "loadSubjects(this)" })</span>
           @Html.ValidationMessageFor(model => model.Standard_Id)
        <br />
        <br />
        <br />

          <span>
            <label class="label" style="margin-left: 100px;">
                Subjects:
            </label>
        </span>

        <span>@Html.DropDownListFor(Model => Model.Subject_Id, Subject, "Select Subjects", new { id = "ddlSubjects", style = "margin:0px 10px 0px 10px;height:25px;width:230px;", onchange = "loadLessons(this)" })</span>
           @Html.ValidationMessageFor(model => model.Subject_Id)
        <span>
            <label class="label" style="margin-left: 90px;">
                Lesson:
            </label>
        </span>
        <span>@Html.DropDownListFor(Model => Model.Lesson_Id, Lesson, "Select Lesson", new { id = "ddlLesson", style = "margin:0px 0px 0px 25px;height:25px;width:230px;" })</span>
           @Html.ValidationMessageFor(model => model.Lesson_Id)
        <br />
        <br />
        <br />


       <span>
            <label class="label" style="margin-left: 56px;">
                Question Type:
            </label>
        </span>
        <span>@Html.DropDownListFor(Model => Model.Question_Type_Id, Question_Type, "Select Question Type", new { id = "ddlQuestion_Type", style = "margin:0px 290px 0px 13px;height:25px;width:230px;" })</span>
           @Html.ValidationMessageFor(model => model.Question_Type_Id)
        <br />
        <br />
        <br />

        <span>
            <label class="label" style="margin-left: 55px;vertical-align:top ! important">
                Enter Question:
            </label>
        </span>


       <span>
            @Html.TextAreaFor(Model => Model.Question, new { id = "txtQuestion", style = "width:450px;margin-left:10px;",onblur="valid();" })
               @Html.ValidationMessageFor(model => model.Question) 
             @*<input type="button" value="Add" onclick="add(document.forms[0].ddlQuestion_Type.value)" />where is your javascript?*@
            <a href="#"><img src="~/images/add-icon.png" onclick="add(document.forms[0].ddlQuestion_Type.value)"  /></a>
            <a href="#" style="font-family:'Segoe UI';font-size:medium;margin-left:-15px;color:#007FC5" onclick="add(document.forms[0].ddlQuestion_Type.value)" >Add Options</a>

             <a href="#" style="margin-left:10px"><img src="~/images/Remove.png" onclick="Remove_Options()"  /></a>
            <a href="#"  style="font-family:'Segoe UI';font-size:medium;color:#007FC5" onclick="Remove_Options()" >Delete Options</a>


       </span>


        <span id="fooBar" style="height: 50px; width: 80px"></span>
        <br />
       <br />
       <br />

    <center>
       <span style="margin-left:10px">
            <input type="submit" value="Save" class="btns" onclick="return CntDynamicTextbox()" />
            <input type="reset" value="Reset" class="btns" onclick="disablediv()" />
            <input type="hidden" name="cntdynamicTxtbox" id="cntdynamicTxtbox"  value=""/>
            <input type="hidden" name="flag" id="flag"  value=""/>
            <input type="hidden" name="RadioTxtboxvalue" id="RadioTxtboxvalue"  value=""/>
            <input type="hidden" name="checkboxTxtboxvalue" id="checkboxTxtboxvalue"  value=""/>

        </span>
        </center>
       <br />
       <br />
       <br />

</div>

}
@使用(Html.BeginForm())
{
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)

课程: @Html.DropDownListFor(Model=>Model.Course_Id,Courses,“选择课程”,新建{Id=“ddlCourse”,style=“左边距:17px;高度:25px;宽度:230px;”,onchange=“loadStandard(this)”) @Html.ValidationMessageFor(model=>model.Course\u Id) 标准: @Html.DropDownListFor(Model=>Model.Standard_Id,Standard,“选择标准”,新建{Id=“ddlStandard”,style=“margin:0px 10px 0px 10px;高度:25px;宽度:230px;”,onchange=“loadSubjects(this)”) @Html.ValidationMessageFor(model=>model.Standard\u Id)


学科: @Html.DropDownListFor(Model=>Model.Subject_Id,Subject,“选择主题”,新建{Id=“ddlSubjects”,style=“margin:0px 10px 0px 10px;高度:25px;宽度:230px;”,onchange=“loadLessons(this)”) @Html.ValidationMessageFor(model=>model.Subject\u Id) 教训: @Html.DropDownListFor(Model=>Model.Lesson_Id,课,“选择课”,新建{Id=“ddlessue”,style=“边距:0px 0px 0px 25px;高度:25px;宽度:230px;”) @Html.ValidationMessageFor(model=>model.Lesson\u Id)


问题类型: @Html.DropDownListFor(Model=>Model.Question_Type_Id,Question_Type,“选择问题类型”,新建{Id=“ddlQuestion_Type”,style=“边距:0px 290px 0px 13px;高度:25px;宽度:230px;”) @Html.ValidationMessageFor(model=>model.Question\u Type\u Id)


输入问题: @Html.TextAreaFor(Model=>Model.Question,新的{id=“txtQuestion”,style=“width:450px;左边距:10px;”,onblur=“valid();”) @Html.ValidationMessageFor(model=>model.Question) @*你的javascript在哪里*@





}
尝试使用
验证摘要(false)
表单是否提交了无效模型?您只是没有看到错误消息吗?摘要和内联消息都没有显示吗?是否在页面顶部有模型声明,但在示例中忽略了它?单击事件时的保存按钮调用CntDynamicTextbox()。此函数是否执行了可能会阻止执行验证的异常操作?没有表单提交正确的模型和CntDynamicTextbox()jst是否用于计算表单上文本框的数量,因为它是动态生成的,并且我看不到任何未内联的消息,甚至摘要表单只是使用空值发布。类中的脚本部分是一个拼写错误,对吗?