验证使用数据注释在mvc 4中不起作用

时间:2014-07-14 08:37:39

标签: 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;

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> 

我的Razor View标记

@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>

}

0 个答案:

没有答案