xVal - 仅针对 ID 字段生成规则
我正在关注这个 帖子。
服务器端验证按预期工作。 但客户端验证器仅为 ID 字段生成。
我的 Linq2Sql 实体类有两个属性 ID 和 ID 。 CategoryName 和下面是我的 Metadata 类
[MetadataType(typeof(BookCategoryMetadata))]
public partial class BookCategory{}
public class BookCategoryMetadata
{
[Required]
[StringLength(50)]
public string CategoryName { get; set; }
}
添加类别的方法
/// <summary>
/// Adds the category.
/// </summary>
/// <param name="category">The category.</param>
public void AddCategory(BookCategory category)
{
var errors = DataAnotationsValidationHelper.GetErrors(category);
if (errors.Any()) {
throw new RulesException(errors);
}
_db.BookCategories.InsertOnSubmit(category);
_db.SubmitChanges();
}
在控制器中创建操作
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Create([Bind(Exclude = "ID")]BookCategory category)
{
try {
_repository.AddCategory(category);
} catch (RulesException ex) {
ex.AddModelStateErrors(ModelState, "");
}
return ModelState.IsValid ? RedirectToAction("Index") : (ActionResult)View();
}
并且视图
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<h2>Create</h2>
<%= Html.ValidationSummary("Create was unsuccessful. Please correct the errors and try again.") %>
<% using (Html.BeginForm()) {%>
<fieldset>
<legend>Fields</legend>
<p>
<label for="CategoryName">CategoryName:</label>
<%= Html.TextBox("CategoryName") %>
<%= Html.ValidationMessage("CategoryName", "*") %>
</p>
<p>
<input type="submit" value="Create" />
</p>
</fieldset>
<% } %>
<div>
<%=Html.ActionLink("Back to List", "Index") %>
</div>
<%= Html.ClientSideValidation<BookCategory>() %>
现在 xVal 只生成 ID 字段的验证规则。
<script type="text/javascript">xVal.AttachValidator(null, {"Fields":[{"FieldName":"ID","FieldRules":[{"RuleName":"DataType","RuleParameters":{"Type":"Integer"}}]}]})</script>
CategoryName 的服务器端验证工作完美。 为什么 xVal 没有为 CategoryName 生成验证规则? 我究竟做错了什么?
I am following this post.
Server side validations work as expected. But the clientside validators are only getting generated for the ID field.
My Linq2Sql Entity Class has two properties ID & CategoryName and below is my Metadata class
[MetadataType(typeof(BookCategoryMetadata))]
public partial class BookCategory{}
public class BookCategoryMetadata
{
[Required]
[StringLength(50)]
public string CategoryName { get; set; }
}
Method to add a category
/// <summary>
/// Adds the category.
/// </summary>
/// <param name="category">The category.</param>
public void AddCategory(BookCategory category)
{
var errors = DataAnotationsValidationHelper.GetErrors(category);
if (errors.Any()) {
throw new RulesException(errors);
}
_db.BookCategories.InsertOnSubmit(category);
_db.SubmitChanges();
}
Create action in the controller
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Create([Bind(Exclude = "ID")]BookCategory category)
{
try {
_repository.AddCategory(category);
} catch (RulesException ex) {
ex.AddModelStateErrors(ModelState, "");
}
return ModelState.IsValid ? RedirectToAction("Index") : (ActionResult)View();
}
And the view
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<h2>Create</h2>
<%= Html.ValidationSummary("Create was unsuccessful. Please correct the errors and try again.") %>
<% using (Html.BeginForm()) {%>
<fieldset>
<legend>Fields</legend>
<p>
<label for="CategoryName">CategoryName:</label>
<%= Html.TextBox("CategoryName") %>
<%= Html.ValidationMessage("CategoryName", "*") %>
</p>
<p>
<input type="submit" value="Create" />
</p>
</fieldset>
<% } %>
<div>
<%=Html.ActionLink("Back to List", "Index") %>
</div>
<%= Html.ClientSideValidation<BookCategory>() %>
Now xVal only generates validation rules for the ID field.
<script type="text/javascript">xVal.AttachValidator(null, {"Fields":[{"FieldName":"ID","FieldRules":[{"RuleName":"DataType","RuleParameters":{"Type":"Integer"}}]}]})</script>
The server side validation for CategoryName works perfect. Why xVal is not generating validation rules for the CategoryName? What am I doing wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
据推测,xVal 0.8 有伙伴类正在运行。 您可以在这里阅读这篇文章:
[http://xval. codeplex.com/Thread/View.aspx?ThreadId=54300][1]
如果这不能解决您的问题,请尝试下载 xVal 的最新代码并修改
xVal.RuleProviders.PropertyAttributeRuleProviderBase:: GetRulesFromTypeCore 另外
,如果这解决了您的问题,您可能需要联系 Steve Sanderson 并让他知道此错误仍然存在。
Supposedly, xVal 0.8 has buddy classes working. You can read this post here:
[http://xval.codeplex.com/Thread/View.aspx?ThreadId=54300][1]
If that doesn't fix your problem, try pulling down the latest code for xVal and modifying
xVal.RuleProviders.PropertyAttributeRuleProviderBase::GetRulesFromTypeCore
to beAlso, if this fixes your problem, you might want to contact Steve Sanderson and let him know this bug is still present.