ASP.NET MVC验证

时间:2010-05-13 13:20:00

标签: asp.net-mvc

我需要验证高级搜索表单,但它有数据要发送给2个不同的表人和文档,因此我无法使用数据注释。我需要验证名称只是字符,帐号和&数量是一个数字。我如何验证这些字段?

<asp:Content ID="indexContent" ContentPlaceHolderID="MainContent" runat="server">
    <center>
        <img src="../../Content/images/DocuVault_Logo.png" alt="DocuVault" />
        <%= Html.ValidationSummary() %>
        <% using (Html.BeginForm("QuickSearch", "Search"))
        { %>
            <div id="div_QuickSearch">
                <table>
                    <tr>
                        <td colspan="2">
                        <%= Html.TextBox("search", "",  new { style = "width: 300px" })%>
                        <input type="submit" value="Search" />
                        </td>
                    </tr>
                    <tr>
                        <td colspan="2">
                        <%= Html.ValidationMessage("search") %>
                        </td>
                    </tr>
                    <tr>
                        <td><a href="#" id="link_advanced">Advanced Search</a></td>
                    </tr>
                </table>
                <br />
            </div>
        <% } %>
        <ul>
        </ul>
        </div>
        <% using (Html.BeginForm("AdvancedSearch", "Search")) 
        { %>
            <div id="div_AdvancedSearch" style="display: none; width: 420px; padding: 10px;">
                People
                <table style="border: solid 1px black; padding: 5px; width: 400px;">
                    <tr>
                        <td>Name:</td>
                        <td align="right">
                        <%= Html.TextBox("searchName") %>
                        <%= Html.ValidationMessage("searchName")%>
                        </td>
                    </tr>
                    <tr>
                        <td>Address:</td>
                        <td align="right">
                        <%= Html.TextBox("searchAddress") %>
                        <%= Html.ValidationMessage("searchAddress")%>
                        </td>
                    </tr>
                    <tr>
                        <td>Account Number:</td>
                        <td align="right">
                        <%= Html.TextBox("searchAccountNumber") %>
                        <%= Html.ValidationMessage("searchAccountNumber")%>
                        </td>
                    </tr>
                </table>
                <br />
                Documents
                <table style="border: solid 1px black; padding: 5px; width: 400px;">
                    <tr>
                        <td>Invoice:</td>
                        <td align="right">
                        <%= Html.TextBox("searchInvoice") %>
                        <%= Html.ValidationMessage("searchInvoice")%>
                        </td>
                    </tr>
                    <tr>
                        <td>Amount:</td>
                        <td align="right">
                        <%= Html.TextBox("searchAmount") %>
                        <%= Html.ValidationMessage("searchAmount")%>
                        </td>
                    </tr>
                    <tr>
                        <td>Job:</td>
                        <td align="right">
                        <%= Html.TextBox("searchJob") %>
                        <%= Html.ValidationMessage("searchJob")%>
                        </td>
                    </tr>
                    <tr>
                        <td>SDI:</td>
                        <td align="right">
                        <%= Html.TextBox("searchSDI") %>
                        <%= Html.ValidationMessage("searchSDI")%>
                        </td>
                    </tr>
                    <tr>
                        <td>Date:</td>
                        <td align="right">
                        <%= Html.TextBox("searchDateBegin", "", new { style = "width: 88px" })%>
                        to
                        <%= Html.TextBox("searchDateEnd", "", new { style = "width: 88px" })%>
                        <%= Html.ValidationMessage("searchDate")%>
                        </td>
                    </tr>
                </table>
                <br />
                <div style="width: 100%;">
                    <span style="float: left; margin-left: 10px;"><a href="#" id="link_quick">QuickSearch</a></span>
                    <span style="float: right; margin-right: 10px;"><input type="submit" value="Advanced Search" /></span>
                </div>
            </div>
        <% } %>
    </center>
    </asp:Content>

2 个答案:

答案 0 :(得分:0)

试试FluentValidation。它是一个非常简单和强大的验证框架,能够根据规则验证您的类。它还可以用于生成ClientSide JQuery Validators

答案 1 :(得分:0)

我使用jquery插件验证并使用我执行验证。因为我想保持简单。