即使我的表单显然无效,验证也不会触发

时间:2010-06-25 00:38:09

标签: .net asp.net-mvc vb.net validation data-annotations

我有以下视图

<% Using Html.BeginForm()%>
<%: Html.ValidationSummary(True) %>
<div class="displayright">
    <h3>
        <%: Html.LabelFor(Function(model) model.About) %></h3>
    <noscript>
        <h3>
            Please use
            <%: Html.ActionLink("Markdown", "Markdown", "About")%>
            to style your input.</h3>
    </noscript>
    <%: Html.TextAreaFor(Function(model) model.About, 5, 5, New With {.class = "user-edit-textarea"})%>
    <div class="wmd-preview">
    </div>
</div>
<%--end left display area--%>
<%--middle display area--%>
<div class="displaymiddle">
    <h3>
        Urban Now User</h3>
    <table width="340">
        <tr>
            <td>
                <%= Html.ValidationSummary("Oops, please correct the errors...") %>
                <%: Html.LabelFor(Function(model) model.UserName)%></td>
            <td class="full-width">
                <%: Html.TextBoxFor(Function(model) model.UserName) %>
                <%: Html.ValidationMessageFor(Function(model) model.UserName) %>
            </td>
        </tr>
        <tr>
            <td>
                <%: Html.LabelFor(Function(model) model.WebSite)%></td>
            <td>
                <%: Html.TextBoxFor(Function(model) model.WebSite) %>
                <%: Html.ValidationMessageFor(Function(model) model.WebSite) %><br />
                <span class="fineprint">http://www.example.com</span> </td>
        </tr>
        <tr>
            <td>
                <%= Html.LabelFor(Function(model) model.BirthDate)%>
            </td>
            <td>
                <%: Html.EditorFor(Function(model) model.BirthDate)%>
                <%: Html.ValidationMessageFor(Function(model) model.BirthDate) %><br />
                <span class="fineprint">never displayed, used to show age.<br />
                    MM/DD/YYYY</span> </td>
        </tr>
        <tr>
            <td>
                <%: Html.LabelFor(Function(model) model.Email)%>
            </td>
            <td>
                <%: Html.TextBoxFor(Function(model) model.Email) %>
                <%: Html.ValidationMessageFor(Function(model) model.Email) %><br />
                <span class="fineprint">never displayed, used for gravatar, optional notifications</span> </td>
        </tr>
        <tr>
            <td>
                <%: Html.LabelFor(Function(model) model.RegionID) %></td>
            <td>
                <%: Html.TextBoxFor(Function(model) model.RegionID) %>
                <%: Html.ValidationMessageFor(Function(model) model.RegionID) %></td>
        </tr>
        <tr>
            <td></td>
            <td>
                <input type="submit" value="Save Profile" />
                <input type="button" id="cancel" name="cancel" value="Cancel" onclick="location.href='<%: Url.Action("Details", "Users", New With {.id = model.ID, .slug = model.UserName})%>'">
            </td>
        </tr>
    </table>
</div>
<%--end middle display area--%>
<% End Using%>

我在我的控制器中使用它:

<AcceptVerbs(HttpVerbs.Get)> _
Function Edit(ByVal id As Integer) As ActionResult
    Dim user As Domain.User = UserService.GetUserByID(id)
    Return View(user)
End Function

<AcceptVerbs(HttpVerbs.Post)> _
Function Edit(ByVal id As Integer, ByVal collection As FormCollection) As ActionResult
    If ModelState.IsValid Then
        Return RedirectToAction("Edit", "Users", New With {.id = 1, .slug = "its valid"})
    End If
End Function

最后我的MetaData如下:

<MetadataType(GetType(UserMetaData))> _
Partial Public Class User : End Class

Public Class UserMetaData

    <DisplayName("name")> _
    <Required(ErrorMessage:="Username is required.")> _
    <StringLength(30, ErrorMessage:="Username cannot exceed 30 characters.")> _
    Public Property UserName As String

    <DisplayName("email")> _
    <StringLength(50, ErrorMessage:="Email Address cannot exceed 50 characters.")> _
    <RegularExpression("^[a-z0-9_\\+-]+(\\.[a-z0-9_\\+-]+)*@[a-z0-9-]+(\\.[a-z0-9+)*\\.([a-z]{2,4})$", ErrorMessage:="Not a valid email address.")> _
    Public Property Email As String

    <DisplayName("website")> _
    <StringLength(256, ErrorMessage:="Web Address cannot exceed 256 characters.")> _
    <RegularExpression("^http(s?)\://[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(/\S*)?$", ErrorMessage:="Not a valid website address")> _
    Public Property WebSite As String

    <DisplayName("about")> _
    <StringLength(4000, ErrorMessage:="Profile cannot exceed 4000 characters.")> _
    Public Property About As String

    <DisplayName("region")> _
    <Required(ErrorMessage:="Region is required.")> _
    Public Property RegionID As Integer

    <DisplayName("birthdate")> _
    <DisplayFormat(ApplyFormatInEditMode:=True, ConvertEmptyStringToNull:=True, DataFormatString:="{0:MM/dd/yyyy}")> _
    Public Property BirthDate As DateTime
End Class

问题在于,当我将空白表单提交回控制器时,我会被重定向到“其有效”页面,这意味着表单通过了验证。

我可能做错了什么?

更新 好的,所以更改控制器中的位现在将我重定向到“无效”页面

<AcceptVerbs(HttpVerbs.Post)> _
Function Edit(ByVal user As Domain.User) As ActionResult
    Dim id As Integer = Url.RequestContext.RouteData.Values("id")
    If Not ModelState.IsValid Then
        Return RedirectToAction("Edit", "Users", New With { .id = id, .slug = "invalid" })
    End If

    Return RedirectToAction("Details", "Users", New With { .id = id })
End Function

然而,我没有得到任何突出显示。

1 个答案:

答案 0 :(得分:1)

尤里卡,我终于挖出了解决方案。这是我的控制器的一个问题。

这是代码

<AcceptVerbs(HttpVerbs.Post)> _
Function Edit(ByVal user As Domain.User) As ActionResult
    Dim id As Integer = Url.RequestContext.RouteData.Values("id")
    If ModelState.IsValid Then
        Return RedirectToAction("Details", "Users", New With {.id = id})
    Else
        Return View(user)
    End If
End Function

问题是最初我没有将Domain.User参数传递给Controller ActionResult。然后在我编辑之后,我发现如果它是无效的,我需要将user传递给视图才能看到突出显示。