2017-04-06 33 views
1

我試圖把我的下拉列表中的jQuery驗證在完整的日曆添加事件,但我不知道爲什麼任何解決方案不工作..誰能幫忙嗎?jquery驗證不工作在我的mvc4項目

HTML代碼:

<div id="add_task_dialog_box" class="hide"> 
<form id="form_data" method="post" class="form-horizontal" style="display: none;"> 
<fieldset class="form-group"> 
    <div class="row class1 " style="padding: 10px;"> 
     <span><span class="col-lg-3 col-md-3 col-sm-3 col-xs-12"> 
      <label for="dd1" style="font-weight: bold; font-size: 13px; font-type: Arial; font-family: Arial Unicode MS"> 
       Project Name</label></span> <span class="col-lg-3 col-md-3 col-sm-3">@Html.DropDownListFor(model => model.Task_runsModel.Project_Id, db.getProjects(a), "---Select---", new { style = "width:200px;", id = "myproject", name = "Project", @class = "required" })</span> 
      @Html.ValidationMessageFor(model => model.Task_runsModel.Project_Id) 
     </span> 
    </div> 

    <div class="row class1 " style="padding: 10px;"> 
     <span><span class="col-lg-3 col-md-3 col-sm-3 col-xs-12"> 
      <label style="font-weight: bold; font-size: 13px; font-type: Arial; font-family: Arial Unicode MS"> 
       Select Task</label></span> <span class="col-lg-3 col-md-3 col-sm-3">@Html.DropDownListFor(model => model.Task_runsModel.Task_Id, db.getTasks(a, c), "--Select--", new { style = "width:200px;", id = "mytask", name = "Task", @class = "required" })</span> 
      @Html.ValidationMessageFor(model => model.Task_runsModel.Task_Id) 
     </span> 
    </div> 
    <div class="row class1" style="padding: 10px;"> 
     <span><span class="col-lg-3 col-md-3 col-sm-3 col-xs-12"> 
      <label style="font-weight: bold; font-size: 13px; font-type: Arial; font-family: Arial Unicode MS"> 
       Task status 
      </label> 
     </span><span class="col-lg-3 col-md-3 col-sm-9 col-xs-12 ">@Html.DropDownListFor(model => model.Task_runsModel.status, db.getStatus(), "---Select---", new { style = "width:200px", id = "taskstatus", name = "Status", @class = "required" }) 
     </span> 
     @Html.ValidationMessageFor(model => model.Task_runsModel.status) 
     </span> 
    </div> 


    <div class="col-lg-12 col-md-3 col-sm-3 col-xs-12"> 
     <div id="chatarea" class="dropdown-toggle" style="float: left; padding-left: 20px; 
      overflow-y: auto; overflow-x: auto;"> 
     </div> 
     <b style="float: right; padding-right: 20px;" class="glyphicon glyphicon-chevron-down" 
      id="DisplayComment"></b> 
    </div> 
    @* <div id="commentdiv" class="dropdown-toggle" style=" padding-left: 20px; overflow-y: auto; overflow-x: auto;width:100%"> 

       <ul id="current_comments" type="none" style="font-weight:bold;"> 
       </ul> 
    </div>*@ 

    <div id="textareadiv" style="padding-top:3px;"> 
     <span class="col-lg-3 col-md-3 col-sm-9 col-xs-12" style="padding-left: 12px; width: inherit;">@Html.TextAreaFor(model => model.Task_runsModel.comment1, new { id = "comment", ariahidden = "true", placeholder = "Write comment", @class = "form-control wp-editor-area", cols = "65", rows = "3", style = " width: calc(100%);" }) </span> 

    </div> 

</fieldset> 
</form> 

的jQuery:

jQuery(function ($) { 


    // $('#form_data').validate(); 

    $("#form_data").validate({  
    messages: { 
     Project_Id: { 
      required: "Eh ? empty username ?", 
      minLength: "At least 4 characters are necessary" 
     }, 
     Task_Id: { 
      required: "password missing" 
     }, 
     status: { 
      required: "please provide us your email, we promise we wont spam !", 
      email: "email id is not in correct format, eg: [email protected]" 
     } 
    } 
}); 
+1

如果你在你的C#模型和JQuery驗證沒有使用的數據註解突兀,你不應該需要手動掛鉤驗證這樣 – Pete

回答

2

裝飾與驗證模型的屬性下面你不需要把它寫在下面jQuery是一個簡單的模型帶有裝飾屬性的類,這稱爲模型驗證的數據註釋

public class UserModel 
{ 
    [Required(ErrorMessage = "Please Enter Name")] 
    [Display(Name = "Name")] 
    public string Name { get; set; } 

    [Required(ErrorMessage = "Please Enter Email Address")] 
    [Display(Name = "Email")] 
    [RegularExpression(@"^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$", 
    ErrorMessage = "Please Enter Correct Email Address")] 
    public string Email { get; set; } 

    [Required(ErrorMessage = "Please Enter Mobile No")] 
    [Display(Name = "Mobile")] 
    [StringLength(10, ErrorMessage = "The Mobile must contains 10 characters", MinimumLength = 10)] 
    public string MobileNo { get; set; } 
} 

要進行MVC驗證工作,請找到THIS ANSWERTHIS博客爲您的MVC項目設置MVC驗證。

下面是一些基本的數據說明可以使用

顯示名稱

一個屬性指定顯示名稱。

DisplayFormat

指定像日期proerty不同格式的屬性的顯示格式。

要求

根據需要指定一個屬性。

ReqularExpression

驗證由指定正則表達式模式的屬性的值。

範圍

的值中的指定範圍驗證屬性的值與。

StringLength

指定的最小和最大長度的字符串屬性。

的MaxLength

指定的字符串屬性最大長度。

+0

模型驗證不使用jQuery – aayushi

+0

工作是它的工作與jQuery,但你不您必須編寫自定義代碼,因爲您只需要修改模型,而其他內容已由MVC和jQuery在內部處理。如果你想知道更多關於這個,請通過[此鏈接](http:// www。c-sharpcorner.com/article/data-annotations-and-validation-in-mvc/) – Curiousdev

+0

我做了同樣的事情,但根據我的知識,jquery不知道我們在模型中進行的驗證的任何內容,這個目的jQuery使用jquery.validate()mrthod但不知道爲什麼它不工作 – aayushi