2009-08-12 3 views
1

다음은이 post입니다.xVal - ID 필드에 대해서만 규칙을 생성합니다.

서버 쪽 유효성 검사가 예상대로 작동합니다. 그러나 클라이언트 측 유효성 검사기는 ID 필드에 대해서만 생성됩니다.

내 Linq2Sql 엔티티 클래스

/// <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(); 
} 

컨트롤러의 동작을 만들기 카테고리를 추가 내 메타 데이터 클래스

[MetadataType(typeof(BookCategoryMetadata))] 
public partial class BookCategory{} 

public class BookCategoryMetadata 
{ 
    [Required] 
    [StringLength(50)] 
    public string CategoryName { get; set; } 
} 

방법을 두 가지 속성 ID & 범주를 가지고 다음과 같습니다

[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에 대한 유효성 검사 규칙을 생성하지 않는 이유는 무엇입니까? 내가 도대체 ​​뭘 잘못하고있는 겁니까?

+0

마지막으로 작성한 댓글을 바탕으로 답을 표시 할 수 있습니까? 감사. – andymeadows

답변

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에게 연락하여이 버그가 아직 있음을 알릴 수 있습니다.

+0

내가 읽었던 게시물에 링크 된 이전 게시물은 오래된 xVal dll을 가졌습니다 .. 최신 xVal 바이너리를 다운로드하면 문제가 해결되었습니다 .. 감사합니다. – Zuhaib