我正在尝试在客户端使用不显眼的验证来添加一些类别实体添加表单。
这是我的实体:
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>
}
现在我将所有输入都保留为emty并尝试验证此表单以使用以下方法获取模型中显示的错误:
$("#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>
答案 0 :(得分:1)
您是否按正确的顺序添加了js文件?
<script src="/Scripts/jquery-1.7.2.min.js" type="text/javascript"></script>
<script src="/Scripts/jquery.validate.min.js" type="text/javascript"></script>
<script src="/Scripts/jquery.unobtrusive-ajax.min.js" type="text/javascript"></script>
在webconfig中:
<appSettings>
<add key="ClientValidationEnabled" value="true" />
<add key="UnobtrusiveJavaScriptEnabled" value="true" />
</appSettings>
答案 1 :(得分:-1)
从视图中删除以下内容
$("#addCategoryForm").validate().valid()
Html.EnableUnobtrusiveJavaScript();
Html.EnableClientValidation();
@Html.ValidationSummary(false)
将以下js文件添加到您的视图中:
<script type="text/javascript" src="../../Scripts/jquery-1.8.2.min.js"></script>
<script type="text/javascript" src="../../Scripts/jquery.validate.min.js"></script>
<script type="text/javascript" src="../../Scripts/jquery.validate.unobtrusive.min.js"></script>
使用
<button type="submit">Create</button>
取代
<input type="submit" value="Create" />
按@Lasse所述添加Web配置部分。 这将导致模型验证抛出验证错误(如果有的话)。