2013-12-16 44 views
0

我有一個mvc 5網站,我試圖使用內置的驗證,它不工作。爲什麼我的驗證消息不顯示?

「不工作」我的意思是驗證消息沒有顯示。我可以在httppost操作上測試模型並驗證它是否有效。那部分工作完美。但我認爲應該在帖子發佈之前進行客戶端驗證。另外,如果沒有,不應該在帖子後面出現錯誤消息嗎?

我錯過了什麼?我已經粘貼了下面的所有相關代碼。

標記具有元素中的驗證數據。

<div id="CommentDateDiv" style="display: none;">Comment Date: 
    <br /> 
    <input data-val="true" data-val-required="The CommentDate field is required." id="CommentDate" name="CommentDate" style="width:150px" type="date" /> 
    <script> 
     jQuery(function() { 
      jQuery("#CommentDate").kendoDatePicker({ 
       "format": "M/d/yyyy", 
       "min": new Date(2013, 11, 1, 0, 0, 0, 0), 
       "max": new Date(2013, 11, 31, 0, 0, 0, 0) 
      }); 
     }); 
    </script> 
</div> 
<div>Comment Details: 
    <br /> 
    <textarea class="k-textbox" cols="20" data-val="true" data-val-required="The CommentDetails field is required." id="CommentDetails" name="CommentDetails" rows="2" style="width: 400px; height: 150px;"></textarea> 
</div> 

這裏是模型

public class CalendarCommentModel 
{ 
    public string CommentType { get; set; } 
    public string EventID { get; set; } 
    [Required] 
    public string CommentDate { get; set; } 
    [Required] 
    [DataType(DataType.Text)] 
    public string CommentDetails { get; set; } 
} 

這裏是標記(使用劍術UI)

@using (Html.BeginForm()) 
{ 
    <div style="display: none;"> 
     <input type="radio" id="EventRadio" name="CommentType" value="event" checked="checked" /><label for="EventRadio">Attach to event</label> 
     <input type="radio" id="LooseCommentRadio" name="CommentType" value="loose" /><label for="LooseCommentRadio">Free Comment</label> 
    </div> 
    <div id="EventSelectorDiv"> 
     Select Event:<br /> 
     @(Html.Kendo().DropDownListFor(x => x.EventID).DataTextField("Text") 
      .DataValueField("Value") 
      .BindTo(@ViewBag.AllEvents).OptionLabel("Select Event...").HtmlAttributes(new { style = "width: 400px;" })) 

    </div> 
    <div id="CommentDateDiv" style="display: none;"> 
     Comment Date:<br /> 
     @(Html.Kendo().DatePicker() 
      .Name("CommentDate") 
      .Min(Convert.ToDateTime(ViewBag.startDate)) 
      .Max(((DateTime)Convert.ToDateTime(ViewBag.endDate)).AddDays(-1)) 
      .HtmlAttributes(new { style = "width:150px" }) 
     ) 
    </div> 
    <div> 
     Comment Details:<br /> 
     @Html.TextAreaFor(x => x.CommentDetails, new { @class = "k-textbox", style = "width: 400px; height: 150px;" }) 

    </div> 
    @(Html.Kendo().Button() 
    .Name("SaveButton") 
    .HtmlAttributes(new { type = "submit" }) 
     .Content("Save Comment")) 
} 

此外,被添加相應的腳本,以及。

<script src="/Scripts/jquery.unobtrusive-ajax.js"></script> 
<script src="/Scripts/jquery.validate.js"></script> 
<script src="/Scripts/jquery.validate.unobtrusive.js"></script> 

回答

2

您需要在窗體中具有驗證摘要或驗證字段。 嘗試添加任何

@Html.ValidationSummary() 

,給你所有的錯誤總結。

或者如果你想要它的個人模型屬性,然後添加像commentdetails之後的東西。

@Html.TextAreaFor(x => x.CommentDetails, new { @class = "k-textbox", style = "width: 400px; height: 150px;" }) 
@Html.ValidationMessageFor(model => model.CommentDetails) 
+0

由於我無法兩次對這個答案投票贊成,所以我投了3倍。謝謝你的幫助。 – Smeegs