Why not create a Service layer that creates the validation for you instead of the user control? See my blog for some more info.
Basically in your Service layer you create MetaData that attaches to the Class Object. I have a LINQ to SQL Class called User, and my UserMetaData adds Validation Functionality to the class.
<MetadataType(GetType(UserMetaData))> _
Partial Public Class User
Public Property UserRegion As String
Public LastSeen As DateTime
End Class
Public Class UserMetaData
<DisplayName("name")> _
<Required(ErrorMessage:="Username is required.")> _
<StringLength(30, ErrorMessage:="Username cannot exceed 30 characters.")> _
<RegularExpression("^\w{3,30}$", ErrorMessage:="Not a valid username.")> _
Public Property UserName As String
<DisplayName("email")> _
<StringLength(50, ErrorMessage:="Email Address cannot exceed 50 characters.")> _
<RegularExpression("^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})$", 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(2000, ErrorMessage:="Profile cannot exceed 2000 characters.")> _
Public Property About As String
<DisplayName("region")> _
<Required(ErrorMessage:="Region is required.")> _
Public Property UserRegion As Integer
<DisplayName("birthdate")> _
<DisplayFormat(ApplyFormatInEditMode:=True, ConvertEmptyStringToNull:=True, DataFormatString:="{0:MM/dd/yyyy}")> _
Public Property BirthDate As DateTime
End Class
Then your validation is as easy as passing the User
object to the View and then using the following for validation
<%
Html.EnableClientValidation()
Using Html.BeginForm("Edit", "Users")
%>
<%-- Lot's more crap in here--%>
<tr>
<td>
<%: Html.LabelFor(Function(model) model.UserName)%></td>
<td class="full-width">
<%: Html.TextBoxFor(Function(model) model.UserName) %>
<%: Html.ValidationMessage("UserName", "*")%>
</td>
</tr>
<%-- Lot's more crap in here--%>
<% End Using%>
<script src="../../Assets/Scripts/MicrosoftAjax.js" type="text/javascript"></script>
<script src="../../Assets/Scripts/MicrosoftMvcAjax.js" type="text/javascript"></script>
<script src="../../Assets/Scripts/MicrosoftMvcValidation.js" type="text/javascript"></script>
EDIT:
Upon reading your "answer", I think I better understand your question. Here's what I think your best option will be.
- You need to create a custom ViewModal for the fields you want to send to the View. In this ViewModal you want to create validation as I did above.
- Then you need to create A view to send the ViewModel to and you need to structure your controller as follows
In your controller you send info to the View
Function Email() As ActionResult
''# do stuff to populate your viewmodel
Return View(customViewModel)
End Function
<AcceptVerbs(HttpVerbs.Post)> _
Function Email(ByVal customViewModel as MyCustomViewModel)
If ModelState.IsValid Then
''# do your valid stuff
Else
Return View(customViewModel)
End If
End Function