2010-05-17 171 views
0

嗨那裏我第一次使用xval,它似乎對所需字段正常工作, 但是我有一些問題 首先它似乎並沒有驗證布爾和客戶端驗證不適用於我,這對我來說不是主要問題,我真正需要工作的是stringlength屬性。它似乎做了一些事情,因爲表單在超出字符串長度時沒有發佈,但是沒有向用戶顯示錯誤消息,這顯然不是我想要的,有沒有人能夠成功完成此操作?asp.net mvc xval驗證

我的模型是這樣的

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 
using System.ComponentModel.DataAnnotations; 
namespace PitchPortal.Core 
{ 
    public class DocumentMetadata 
    { 

     //[Required] 
     // public bool visibility { get; set; } 


     [Required,StringLength(10, ErrorMessage = "title is too long")] 
     public string title { get; set; } 

     [Required, StringLength(10, ErrorMessage = "description is too long")]  
     public string description { get; set; } 

     [Required, StringLength(10, ErrorMessage = "summary is too long")]  
     public string summary { get; set; } 

    } 
} 

的HTML是這樣

<div id="results" title="Upload results"/> 
    <form id="myForm" action="<%=Url.Action("New") %>" method="post" enctype="multipart/form-data"> 
    <% Html.EnableClientValidation(); %> 

    <%= Html.ValidationSummary() %> 
    <table> 
     <tr> 
       <td> <%=Html.Label("File")%></td> 
       <td> 
        <input type="file" id="file1" name="fileUpload" /> <br /> 
        <%=Html.SubmitButton<DocumentController>(x => x.Upload(), "GetImage", "")%> 
       </td> 
       <td> 
        <%=Html.ValidationMessage("file1")%> 
       </td> 
     </tr> 
     <tr> 
       <td> <%=Html.Label("Visible")%></td> 
       <td> 
        <%= Html.RadioButton("visibility",true,true) %>true 
        <%= Html.RadioButton("visibility", false)%>false 
       </td> 
       <td> 
        <%= Html.ValidationMessage("visibility")%> 
       </td> 

     </tr> 
     <tr> 
       <td> <%=Html.Label("Title")%></td> 
       <td> <%=Html.TextBox("doc.title")%></td>   
       <td> <%= Html.ValidationMessage("doc.title")%></td> 
     </tr> 
     <tr> 
       <td> <%=Html.Label("Description")%></td> 
       <td><%= Html.TextArea("doc.description")%></td> 
       <td><%= Html.ValidationMessage("doc.description")%></td> 
     </tr> 
     <tr> 
       <td> <%=Html.Label("Summary")%></td> 
       <td> <%= Html.TextArea("doc.summary")%></td> 
       <td> <%= Html.ValidationMessage("doc.summary")%></td> 
     </tr> 
     <tr> 
       <td> <%=Html.Label("Filetype")%></td> 
       <td> <%= Html.DropDownList("Filetype_id", (IEnumerable<SelectListItem>)ViewData.Model.AllFiletypesList)%> </td> 

       <td> <%= Html.ValidationMessage("doc.Filetype_id")%> </td> 
     </tr> 
     <tr> 
       <td> <%=Html.Label("Category")%></td> 
       <td><%= Html.DropDownList("cat.parent_id", (IEnumerable<SelectListItem>)ViewData.Model.AllCategoriesList, "-please select item-", new { className = "unselected" })%> </td> 
      <td><%= Html.ValidationMessage("cat.parent_id")%> </td> 
     </tr> 

     <% 
      if (Session["TempFolder"] == null)  
      { 
       for (int i = 1; i < 6; i++) 
       { %> 
        <tr> 
         <td> <%=Html.Label("Shot "+i.ToString()) %> </td> 
         <td><input type="file" id="image_<%= i.ToString() %>" name="image_<%= i.ToString() %>" /></td> 
        </tr> 
     <%  } 
      }%> 

     <tr> 
       <td><input type="submit" value="save"/></td> 
     </tr> 
</table> 
</form>  
</div> 

爲部分類的代碼是在這裏

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Data.Linq; 
using System.Web; 
using System.IO; 
using System.Configuration; 
using xVal.ServerSide; 
using System.Web.Mvc; 
using PitchPortal.Core.Repositories; 
using System.Web.Script.Serialization; 
using System.ComponentModel.DataAnnotations; 
using PitchPortal.Core.Extensions; 
namespace PitchPortal.Core 
{ 
    [MetadataType(typeof(DocumentMetadata))] 

    public partial class Document : IPostedFile 
    { 

     IRepository<FileType> IFiletypeRepository = new Repository<FileType>(new DataContextProvider(new ConnectionStringProvider(ConfigurationManager.AppSettings["ConnectionString"]))); 
     static ILoggingService logger = Microsoft.Practices.ServiceLocation.ServiceLocator.Current.GetInstance<ILoggingService>(); 


     public int DownloadCounter 
     { 
      get 
      { 
       return this.Downloads1.Count; 
      } 
     } 

     [ScriptIgnore] 
     public bool IsNewDocument 
     { 
      get { return this.document_id<1; } 
     } 


     public string clientClassPath 
     { 
      get { return "DocumentVO"; } 
     } 

     public string VersionGuid 
     { 
      get; 
      set; 
     } 

     [ScriptIgnore] 
     public virtual HttpPostedFileBase PostedFile 
     { 
      get; 
      set; 
     } 
     [ScriptIgnore] 
     public string BasePath 
     { 


      get 
      { 


       return PathExtensions.Build(new string[] { ConfigurationManager.AppSettings["Root"], Category1.GetFamilyTreePath(), title }); 


      } 


     } 




    } 


} 

回答

0

你忘了:

<%= Html.ClientSideValidation("doc", typeof(Document)) 
     .UseValidationSummary("validationSummary") %> 

,並把你的<%= Html.ValidationSummary() %> bettween tags <div id="validationsummary">and </div>

+0

謝謝葛瑞格爾,對於客戶端驗證工作(非常感謝,你不知道它意味着多少對我來說),但完全不是那麼回事排序stringlength問題,我的理解是不是爲了。你知道有什麼修復嗎? – mctayl 2010-05-17 18:07:32

+0

你能發佈你的文檔類代碼嗎? – Gregoire 2010-05-17 18:11:32

+0

我正在使用Linq和好友/元數據類,這是我之前發佈的類。謝謝 – mctayl 2010-05-17 18:21:02