Public Interface ICustomerView
Property Name As String
...... 'other props'
End Interface
<ComponentModel.DataAnnotations.MetadataType(GetType(CustomerViewMetadata))>
Public Class CustomerView
implements ICustomerView
End Class
Public Class CustomerViewMetadata
<Required()>
Public Property Name As String
......'other props'
End Class
the data is diplayed on the form by the following model class:
Public Class EditCustomerView
Public Property Customer As ICustomerView
'other props'
End Class
the problem is that only server side validation works fine.
the client side validation doesn't work.
I think that the problem lies in the interface, but for other complex reasons I need to code against it.
UPDATE
what I'm asking is very similar to what is posted
here
1) If you are creating your model manually (not generated by a data framework), you'll need to add data annotations to any model property that you want to validate. Example:
[Required]
public string Name { get; set; }
2) The easiest way to test validation in your controller action is using ModelState.IsValid:
if (ModelState.IsValid)
{
// perform your code actions
}
else {
return View(YourModel); // it will return your View with the data and also validation errors
}
3) Add validation markup to your form
@Html.ValidationSummary(...)
@using (Html.BeginForm())
{
@Html.TextBoxFor(m => m.Name)
@Html.ValidationMessageFor(m => m.Name)
// add other form elements with same formatting
<input type="Submit" value="Update" />
}
Do you have jquery validate files referenced in your project and view or layout file.
<script src="@Url.Content("~/Scripts/jquery.validate.min.js")"
type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")"
type="text/javascript></script>
Yes I only show you the server side aspect of my code because I think the problem lies there, but all client side code has what is expected
Yes I only show you the server side aspect of my code because I think the problem lies there, but all client side code has what is expected
Above code seems OK, Im not extremely sure where is the error of your issue, but if you can post the View code to us, it will helpful for us to work with your issue.
Regards
Please mark the replies as answers if they help or unmark if not.
Feedback to us
Yes I only show you the server side aspect of my code because I think the problem lies there, but all client side code has what is expected
Above code seems OK, Im not extremely sure where is the error of your issue, but if you can post the View code to us, it will helpful for us to work with your issue.
Regards
ok,
this is the controller:
Function Create(ByVal collection As FormCollection) As ActionResult
Dim model As New EditCustomerView
model.Customer = New CustomerView
Try
If Not TryUpdateModel(Of EditCustomerView)(model) Then
Throw New EntityValidationException
End If
vaizeman
Member
57 Points
78 Posts
dataannotation and client side validation
Nov 08, 2012 12:21 PM|LINK
Hi.
I need to validate my model class over client:
the data is diplayed on the form by the following model class:
the problem is that only server side validation works fine.
the client side validation doesn't work.
I think that the problem lies in the interface, but for other complex reasons I need to code against it.
UPDATE
what I'm asking is very similar to what is posted here
how can I solve this?
adeelehsan
All-Star
18297 Points
2740 Posts
Re: dataannotation and client side validation
Nov 08, 2012 12:29 PM|LINK
See the following link:
http://blogs.microsoft.co.il/blogs/gilf/archive/2010/09/14/how-to-enable-client-side-validation-in-asp-net-mvc-2.aspx
MCPD ASP.NET 4.0 and 3.5, MCTS WSS, MOSS, SharePoint 2010, MCT
Microsoft Community Contributor Award 2011
JohnLocke
Contributor
3216 Points
710 Posts
Re: dataannotation and client side validation
Nov 08, 2012 12:34 PM|LINK
1) If you are creating your model manually (not generated by a data framework), you'll need to add data annotations to any model property that you want to validate. Example:
[Required] public string Name { get; set; }2) The easiest way to test validation in your controller action is using ModelState.IsValid:
if (ModelState.IsValid) { // perform your code actions } else { return View(YourModel); // it will return your View with the data and also validation errors }3) Add validation markup to your form
@Html.ValidationSummary(...) @using (Html.BeginForm()) { @Html.TextBoxFor(m => m.Name) @Html.ValidationMessageFor(m => m.Name) // add other form elements with same formatting <input type="Submit" value="Update" /> }vaizeman
Member
57 Points
78 Posts
Re: dataannotation and client side validation
Nov 08, 2012 12:42 PM|LINK
thanks. but it doesn't help in my context
CPrakash82
All-Star
18290 Points
2844 Posts
Re: dataannotation and client side validation
Nov 08, 2012 12:50 PM|LINK
Do you have jquery validate files referenced in your project and view or layout file.
<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script> <script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript></script>
vaizeman
Member
57 Points
78 Posts
Re: dataannotation and client side validation
Nov 08, 2012 12:57 PM|LINK
Yes I only show you the server side aspect of my code because I think the problem lies there, but all client side code has what is expected
Young Yang -...
All-Star
21343 Points
1818 Posts
Microsoft
Re: dataannotation and client side validation
Nov 09, 2012 01:18 AM|LINK
Above code seems OK, Im not extremely sure where is the error of your issue, but if you can post the View code to us, it will helpful for us to work with your issue.
Regards
Feedback to us
Develop and promote your apps in Windows Store
vaizeman
Member
57 Points
78 Posts
Re: dataannotation and client side validation
Nov 09, 2012 07:28 AM|LINK
ok,
this is the controller:
Function Create(ByVal collection As FormCollection) As ActionResult Dim model As New EditCustomerView model.Customer = New CustomerView Try If Not TryUpdateModel(Of EditCustomerView)(model) Then Throw New EntityValidationException End Ifand this is in the view:
@ModelType EditCorrezioneTemperaturaView <script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script> <script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script> @Using Html.BeginForm() @Html.ValidationSummary(False) <div class="editor-label"> @Html.LabelFor(Function(m) m.User.Name) </div> <div class="editor-field"> @Html.TextBoxFor(Function(m) m.User.Name) @Html.ValidationMessageFor(Function(m) m.User.Name) </div> ............... ................ End Usingthe problem, I think, lies in the model code.
if I code against a concrete class (CutomerView) all works fine. When I code against Interface (ICustomerView) validation on the client is not working
RameshRajend...
Star
7983 Points
2099 Posts
Re: dataannotation and client side validation
Nov 09, 2012 07:44 AM|LINK
http://odetocode.com/Blogs/scott/archive/2011/02/22/custom-data-annotation-validator-part-ii-client-code.aspx
http://stackoverflow.com/questions/6987490/client-side-custom-data-annotation-validation