0
我在查看數據驗證時遇到問題。 我相信'AssetName'應該顯示一條錯誤消息,如果用戶在文本框中標籤並且沒有放入任何數據,但是當文本框失去焦點時不顯示任何消息。驗證數據無法正常工作(DataAnnotations)
型號:
using System.ComponentModel.DataAnnotations;
namespace ACore.Models
{
public class AssetForm
{
[Required]
public string AssetName { get; set; }
public string Address1 { get; set; }
public string Address2 { get; set; }
public string City { get; set; }
[UIHint("StatesEditor")]
public State State { get; set; }
[Required(ErrorMessage = "TEST")]
public string ZipCode { get; set; }
[Required]
public string Seg3Code { get; set; }
}
}
查看:
@using System.Web.Optimization
@model ACore.Models.AssetForm
@using (Html.BeginForm(null, null, FormMethod.Post, new { id = "frmAsset" }))
{
<div class="tempStyle">
<div class="editor-label fl">
@*@Html.LabelFor(model => model.AssetName)*@
Asset Name:
</div>
<div class="editor-field">
@Html.EditorFor(model => model.AssetName)
@Html.ValidationMessageFor(model => model.AssetName)
</div>
</div>
<div class="tempStyle">
<div class="editor-label">
Address 1:
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Address1)
@Html.ValidationMessageFor(model => model.Address1)
</div>
</div>
}
的Web.config
第<appSettings>
<add key="webpages:Version" value="2.0.0.0" />
<add key="webpages:Enabled" value="true" />
<add key="PreserveLoginUrl" value="true" />
<add key="ClientValidationEnabled" value="true" />
<add key="UnobtrusiveJavaScriptEnabled" value="true" />
</appSettings>
我結束了添加以下代碼,但現在看來,班裏DataAnnotations對驗證沒有影響。我寧願通過DataAnnotations來控制驗證:
// Validate the Maintenance and Office percentages.
var validator = $("#frmAsset").validate({
rules: {
AssetName: {
required: true
},
Seg3Code: {
required: true,
minlength: 3,
maxlength: 3
}
},
messages: {
AssetName: " ✖ Required",
Seg3Code: " ✖ Required"
},
submitHandler: function (form) {
$.ajax({
type: "POST",
url: "/Asset/Create",
data: {
},
//data: $("form").serialize(),
success: function (data) {
//console.log(data);
// Close popup window
var window = $('#AssetEditorPopUp').data("kendoWindow");
window.close();
// Refresh grid to show changes
$('#grid').data("kendoGrid").dataSource.read();
},
error: function() {
alert("There was an error editing the asset.");
}
});
return false; // to block page redirect since you're using ajax
}
你有你的觀點稱Jquery的庫?另外我不認爲所需的屬性立即驗證。如果該字段爲空,那麼ModelState只在發佈後發生錯誤 – 2013-05-06 18:03:05
我的_Layout.cshtml中添加了我的jQuery庫。我也嘗試添加jquery.min.js和jquery.validate.js到頁面沒有運氣。 – Mithrilhall 2013-05-06 18:07:06
當用戶跳出空白文本框時,應該顯示該消息,這是必需的,而不是標籤。還是你只是誤解你的句子? – 2013-05-06 18:27:32