2012-12-10 26 views
0

我正在(最後)潛入JQuery中,並且想知道如何正確處理文本輸入的發佈前的驗證。我有一個ASPX頁面,它會遍歷我的模型的一部分,爲每個循環調用一個用戶控件。我也有一個JQuery添加鏈接,將添加一個新的(空白)用戶控件集合。在foreach循環中進行ASP.NET MVC2驗證

頁的樣子:

<h1>Create Document List</h1> 

    <% Html.EnableClientValidation(); %> 

    <% using (Html.BeginForm("CreateList", "Documents", FormMethod.Post, new { @class = "list", @enctype = "multipart/form-data" })) 
    { %> 
     <%= Html.AntiForgeryToken() %> 

     <div id="editorRows"> 
      <% foreach(var q in ViewData.Model.DocumentAndFileList) 
        Html.RenderPartial("DocEditRow", q); 
      %> 
     </div> 
     <br /> 

     <%= Html.ActionLink("Add another...", "Add", null, new { id = "addItem" }) %> 

     <br /> 
     <br /> 
     <br /> 

     <%= Html.SubmitButton("btnSave", "Save Document List")%> 
     <%= Html.Button("btnCancel", "Cancel", HtmlButtonType.Button, "window.location.href = '" + Html.BuildUrlFromExpressionForAreas<DocumentsController>(c => c.Index()) + "';") %> 
    <% } %> 
</asp:Content> 

用戶控件的樣子:

<div class="editorRow"> 
    <% using (Html.BeginCollectionItem("docs")) 
     { %> 

     <%= Html.Hidden("Document.Id", (Model != null) ? Model.Id : 0)%> 

     <label for="Number">Document Name:</label> 
     <%= Html.TextBox("Number", (Model != null) ? Model.Number : "", new { @size = "50", @maxlength = "255" })%> 
     <%= Html.ValidationMessageFor(m => m.Number) %> 

     &nbsp; 

     <% if (Model != null && Model.FileName != null && Model.FileName.Length > 0) 
      { %> 
      <label>Current File:</label> 
      <%= Model.FileName%> 
     <% } 
      else 
      { %> 
      <label> 
       File Upload: 
       <%= Html.FileBoxFor(m => m.HttpPostedFileBase)%> 
      </label> 
     <% } %> 
     <a href="#" class="deleteRow">delete</a> 
    <% } %> 
</div> 

我也跟着上his blog舊的文章他的斯科特谷的意見。出於某種原因,我不是得到任何驗證。這個項目的以前的開發人員使用了很多JQuery,所以我現在試圖讓JQuery驗證工作,但不知道從哪裏開始。谷歌搜索似乎使我以約500種不同的方式來做到這一點。任何指針將不勝感激。

TIA


注:

我加入以下到我的網頁:

<asp:Content ID="myScript" ContentPlaceHolderID="pageScript" runat="server"> 
    <script type="text/javascript" language="javascript"> 
     $(document).ready(function() { 
      $("#LoadDocs").validate(); 
      $.validator.unobtrusive.parse("#LoadDocs"); 
     }); 
    </script> 
</asp:Content> 

這似乎是部分工作。如果我添加了幾個新行到我的集合,只有第一行將彈出消息,如果文本框爲空。我還添加了

類= 「需要」

我所有的投入。

雖然我仍然努力讓它工作100%。有什麼建議麼?

+0

你見過這個問題:http://stackoverflow.com/questions/2500371/asp-net-mvc-enableclientvalidation-doesnt-work與腳本列入秩序文件? –

+0

我有正確順序的內含物。我也想知道如何在我調用(AJAX)Add方法時添加驗證。 –

回答

0

我從來沒有想出一種方法來使用內置的驗證器。我落得這樣做是這樣的:

<asp:Content ID="myScript" ContentPlaceHolderID="pageScript" runat="server"> 
    <script type="text/javascript" language="javascript"> 
     $().ready(function() { 
      $("#addItem").click(function() { 
       //alert('hi'); 

       $.ajax({ 
        url: this.href, 
        cache: false, 
        success: function (html) { $("#editorRows").append(html); } 
       }); 
       return false; 
      }); 

      $("a.deleteRow").live("click", function() { 
       $(this).parents("div.editorRow:first").remove(); 
       return false; 
      }); 
     }); 

     function OnDocUpload() { 
      var editorRows = $('.editorRow'); 
      //alert(editorRows.length); 
      var isGood = true; 
      editorRows.each(function() { 
       var input = $(this).children('.required.input').first(); 
       var msg = $(this).children('.display_message').first(); 
       //alert(input.val().length + " | " + msg.html); 
       if (input.val().length < 1) { 
        isGood = false; 
        input.css('background-color', '#FEADCE'); 
        msg.text("A document name is required!"); 
       } else { 
        input.css('background-color', '#FAFAD2'); 
        msg.text(""); 
       } 
      }); 
      return isGood; 
     } 

     function SubmitForm() { 
      if (OnDocUpload()) { 
       var frm = $("#LoadDocs"); 
       frm.submit(); 
       //$("#LoadDocs").submit(); 
      } 
     } 
    </script> 
</asp:Content> 

<asp:Content ID="Content1" ContentPlaceHolderID="MainContentPlaceHolder" runat="server"> 

    <h1>Create Document List</h1> 

    <% 
    Html.EnableClientValidation(); 
    using (Html.BeginForm("CreateList", "CIPDocuments", FormMethod.Post, new { @class = "wufoo", @enctype = "multipart/form-data", @id = "LoadDocs" })) 
    { %> 
     <%= Html.AntiForgeryToken() %> 

     <div id="editorRows"> 
      <% foreach(var cipDocument in ViewData.Model.CIPDocumentAndFileList) 
        Html.RenderPartial("DocEditRow", cipDocument); 
      %> 
     </div> 
     <br /> 

     <%= Html.ActionLink("Add another...", "Add", null, new { id = "addItem" }) %> 

     <br /> 
     <br /> 
     <br /> 

     <%= Html.Button("btnSave", "Save Document List", HtmlButtonType.Button, "SubmitForm();")%> 
     <%= Html.Button("btnCancel", "Cancel", HtmlButtonType.Button, "window.location.href = '" + Html.BuildUrlFromExpressionForAreas<CIPDocumentsController>(c => c.Index()) + "';") %> 
    <% } %> 
</asp:Content>