【问题标题】:asp.net mvc xval validationasp.net mvc xval 验证
【发布时间】:2010-05-17 17:19:02
【问题描述】:

您好,我是第一次使用 xval,它似乎对必填字段工作正常, 但是我遇到了一些问题 首先,它似乎没有验证布尔值,而且客户端验证对我不起作用,这对我来说不是一个主要问题,我真正需要工作的是 stringlength 属性。它似乎做了一些事情,因为超过字符串长度时没有发布表单,但是没有向用户显示错误消息,这显然不是我想要的,有没有人能够成功地做到这一点?

我的模型是这样的

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


            }


        } 




    }


}

【问题讨论】:

    标签: asp.net asp.net-mvc xval


    【解决方案1】:

    你忘了放:

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

    并把你的&lt;%= Html.ValidationSummary() %&gt; bettween tags &lt;div id="validationsummary"&gt;and &lt;/div&gt;

    【讨论】:

    • 感谢 Gregoire,它适用于客户端验证(非常感谢,您不知道这对我意味着什么)但不是字符串长度问题,我知道这不是为了.你知道有什么解决办法吗?
    • 我正在使用 Linq 和一个好友/元数据类,这是我之前发布的类。谢谢
    • 我还发布了扩展 linq 生成类的部分类
    • 我不太理解你的字符串长度问题,你的问题是在客户端还是在服务器端?如果它在服务器端,您需要具有验证您的对象的功能检查此页面上的 DataValidationRunner:blog.stevensanderson.com/category/xval/page/2
    • 感谢您的回复,这一定是我的错误,我以为 StringLength 和 Range 开箱即用,不知道您需要在模型类中编写代码才能做到这一点。我的假设是否正确?理想情况下,我希望它适用于客户端和服务器端,位服务器端更重要
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-04
    • 2011-03-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多