我有一个由Linq2Sql生成的类。我尝试将客户端验证添加到其属性中,因此我执行以下操作:
[MetadataType(typeof(ResourceMetadata))]
public partial class Resource
{
// Resource is a class in the LINQ to SQL generated data context.
}
public class ResourceMetadata
{
[Required(ErrorMessage = "error !")]
public string NewsTitle { get; set; } // the NewsTitle property is also in generated Resource class
}
然后,在我的视图中,
<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>
<%Html.EnableClientValidation(true); %>
<% using (Html.BeginForm()) { %>
<%: Html.ValidationSummary(true) %>
<div class="editor-label">
<%: Html.LabelFor(model => model.NewsTitle) %>
</div>
<div class="editor-field">
<%: Html.EditorFor(model => model.NewsTitle) %>
<%: Html.ValidationMessageFor(model => model.NewsTitle) %>
</div>
<input type="submit" value="Create" />
<% } %>
Web.config文件
的appSettings
add key="webpages:Version" value="1.0.0.0" add key="ClientValidationEnabled" value="true" add key="UnobtrusiveJavaScriptEnabled" value="true"
的appSettings
但是,验证不起作用,没有显示,模型被传递到服务器。为什么?
答案 0 :(得分:1)
因为您尚未在“查看”页面中启用。
使用此
Html.ValidationSummary(true) // you have set it false