views:

52

answers:

1

I've got the following View

        <% 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&lt;/span&gt; </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%>

And I'm using this in my controller

<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

And finally I have my MetaData as follows

#Region "Validation"
    <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
#End Region

The problem is that when I submit a blank form back to the controller, I get redirected to the "its valid" page, meaning that the form passed validation.

What might I be doing wrong?

EDIT:

Ok so changing the bit in my controller is now redirecting me to the "invalid" page

<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

However I'm not getting any highlighting.

A: 

Eureka, I finally dug up the solution. It was an issue with my Controller.

Here's the code

<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

The issue was that initially I didn't have a Domain.User parameter being passed to the Controller ActionResult. Then after my edit, I discovered that if it was INVALID, that I needed to pass that user back to the View in order to see highlighting.

rockinthesixstring