我正在尝试在客户端创建一些具有不显眼验证的类别实体添加表单。
这是我的实体:
public class Category
{
public Int32 Id { get; set; }
[DisplayName("Alias")]
[Required(AllowEmptyStrings = false, ErrorMessage = "Have to complete")]
[StringLength(100, ErrorMessage = "asdasdasd", MinimumLength = 3)]
public String Name { get; set; }
[DisplayName("Name1")]
[Required(AllowEmptyStrings = false, ErrorMessage = "Have to complete")]
public String DisplayName { get; set; }
[DisplayName("Name2")]
[Required(AllowEmptyStrings = false, ErrorMessage = "Have to complete")]
public String DisplayNameTZK { get; set; }
[DisplayName("Url")]
[DataType(DataType.Url, ErrorMessage = "Url")]
public String Uri { get; set; }
public Guid AddingGuid { get; set; }
public Boolean IsActive { get; set; }
...
}
这是我的看法:
@model HSDT.Models.Entities.Category
@{
Html.EnableUnobtrusiveJavaScript();
Html.EnableClientValidation();
}
@using (Html.BeginForm("", "", null, FormMethod.Post, new { id = "addCategoryForm", name = "addCategoryForm" }))
{
@Html.AntiForgeryToken()
@Html.ValidationSummary(false)
<fieldset>
<legend>Category</legend>
<div class="editor-label">
@Html.LabelFor(model => model.Name)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Name)
@Html.ValidationMessageFor(model => model.Name)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.DisplayName)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.DisplayName)
@Html.ValidationMessageFor(model => model.DisplayName)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.DisplayNameTZK)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.DisplayNameTZK)
@Html.ValidationMessageFor(model => model.DisplayNameTZK)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Uri)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Uri)
@Html.ValidationMessageFor(model => model.Uri)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.AddingGuid)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.AddingGuid)
@Html.ValidationMessageFor(model => model.AddingGuid)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.IsActive)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.IsActive)
@Html.ValidationMessageFor(model => model.IsActive)
</div>
<p>
<input type="submit" value="Create" />
</p>
</fieldset>
}
现在,我将所有输入都留空,并尝试验证此表单以获取模型中显示的错误:
$("#addCategoryForm").validate().valid()
但是我得到true
了(并且没有错误),但应该是false
因为在数据注释中我[Required]
为几个字段添加了属性。我做错了什么?
感谢您的任何提前。
编辑-1
这是我的配置:
<appSettings>
<add key="ClientValidationEnabled" value="true" />
<add key="UnobtrusiveJavaScriptEnabled" value="true" />
</appSettings>
这里是参考:
<script src="@Url.Content("~/Scripts/jquery-1.6.2.min.js")" type="text/javascript"></script>
<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>