2015-11-16 92 views
2

我有一個文本框,並且希望顯示提交表單時所需的字段。如果我點擊文本框和選項卡轉到下一個文本框,會出現錯誤消息。我有 '必要' 的數據標註在我的模型是這樣的:使用Kendo UI在ASP MVC中進行必需的字段驗證

[DisplayName("Shipment Number")] 
[Required] 
public string ShipmentNumber   { get; set; } 

我的視圖看起來是這樣的:

$("form").kendoValidator(); 

$.validator.setDefaults({ 
     ignore: "" 
}); 

<div class="col-sm-8"> 
    @Html.TextBoxFor(model => model.ShipmentNumber, new {@class = "form-control", @maxlength = "8", @title = "Maximum length is 8"}) 
    @Html.ValidationMessageFor(model => model.ShipmentNumber) 
</div> 

哦,我也是在$功能添加了這些

當我將文本框留空並提交表單時,表單被提交而不顯示錯誤消息,即'貨件編號是必需的!'。 P.S:我也試過DataAnnotation:

[Required(ErrorMessage = "ShipmentNumber Is Required")] 

有誰知道我能做到這一點?

更新: 這個固定: 變化剃刀代碼,使所需的文本框,並添加所需的錯誤消息。

@Html.TextBoxFor(model => model.ShipmentNumber, new {@class = "form-control", @maxlength = "8", @title = "Maximum length is 8", data_required_msg = "Shipment Number is required!", required = "required" }) 

在按鈕在JavaScript代碼提交,這樣做:

var validator = $(".form-horizontal").kendoValidator().data("kendoValidator"); 
if (validator.validate()) { 
    //code that needs to be executed if it is valid 
} 

注:我也得到了來自莫代爾類,@ Html.ValidationMessageFor和$( 「形式」)去掉註釋數據的kendoValidator碼。

回答

1

嘗試強制驗證,當您點擊按鈕:

// Validate the input when the Save button is clicked 
$("#save").on("click", function() { 
    if (validator.validate()) { 
     // If the form is valid, the Validator will return true 
     save(); 
    } 
}); 

$("form").submit(function(event) { 
    event.preventDefault(); 
    if (validator.validate()) { 
     status.text("Hooray! Your tickets has been booked!") 
       .removeClass("invalid") 
       .addClass("valid"); 
    } else { 
     status.text("Oops! There is invalid data in the form.") 
       .removeClass("valid") 
       .addClass("invalid"); 
    } 
}); 

http://docs.telerik.com/kendo-ui/framework/validator/overview

+0

史蒂夫,好像然而,要工作就說明 '提示' 信息,即'最大長度是8',而不是數據註釋錯誤消息。任何想法爲什麼? – sanjeev40084

+0

這來自TextBoxFor上的標題htmlattribute。 –

+0

我明白,但我需要在用戶懸停在文本框上時顯示工具提示。 – sanjeev40084

相關問題