xVal - 仅为ID字段生成规则

时间:2009-08-12 19:42:39

标签: c# asp.net-mvc data-annotations xval

我正在关注此post

服务器端验证按预期工作。但是客户端验证器只是为ID字段生成的。

我的Linq2Sql实体类有两个属性ID& CategoryName及以下是我的元数据类

[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生成验证规则?我做错了什么?

1 个答案:

答案 0 :(得分:1)

据说,xVal 0.8有好友等级。你可以在这里阅读这篇文章:

[http://xval.codeplex.com/Thread/View.aspx?ThreadId=54300][1]

如果这不能解决您的问题,请尝试下拉xVal的最新代码并将xVal.RuleProviders.PropertyAttributeRuleProviderBase::GetRulesFromTypeCore修改为

 protected override RuleSet GetRulesFromTypeCore(Type type)
 {
   var typeDescriptor = metadataProviderFactory(type).GetTypeDescriptor(type);
   var rules = (from prop in typeDescriptor.GetProperties().Cast<PropertyDescriptor>()
                         from rule in GetRulesFromProperty(prop)
                         select new KeyValuePair<string, Rule>(prop.Name, rule));

   var metadataAttrib = type.GetCustomAttributes(typeof(MetadataTypeAttribute), true).OfType<MetadataTypeAttribute>().FirstOrDefault();
   var buddyClassOrModelClass = metadataAttrib != null ? metadataAttrib.MetadataClassType : type;
   var buddyClassProperties = TypeDescriptor.GetProperties(buddyClassOrModelClass).Cast<PropertyDescriptor>();
   var modelClassProperties = TypeDescriptor.GetProperties(type).Cast<PropertyDescriptor>();

   var buddyRules =  from buddyProp in buddyClassProperties
                              join modelProp in modelClassProperties on buddyProp.Name equals modelProp.Name
                              from rule in GetRulesFromProperty(buddyProp)
                              select new KeyValuePair<string, Rule>(buddyProp.Name, rule);

   rules = rules.Union(buddyRules);
   return new RuleSet(rules.ToLookup(x => x.Key, x => x.Value));
 }

此外,如果这可以解决您的问题,您可能需要联系Steve Sanderson并让他知道此错误仍然存​​在。