即使我的表单明显无效,验证也不会触发
我有以下视图
<% 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
最后我的元数据如下:
<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
但是我没有得到任何突出显示。
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</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%>
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:
<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
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?
Update:
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尤里卡,我终于找到了解决方案。这是我的控制器的问题。
这是代码
问题是最初我没有将
Domain.User
参数传递给控制器 ActionResult。然后在编辑后,我发现如果它无效,我需要将该用户
传递回视图才能看到突出显示。Eureka, I finally dug up the solution. It was an issue with my Controller.
Here's the code
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 thatuser
back to the View in order to see highlighting.