I have a strongly typed view, which has some input boxes to map to a collection in an entity. Say for an example , take case of a View for adding an Employee detail and within that there are input fields to enter department name as well (say 2). Both of
them are required.
Here is the class structure of these two entities:
public class Employee
{
public int EmployeeID{get;set;}
public string Name {get;set; }
public IList<Department> DepartmentList{get;set;}
}
public class Deparment {
public string Name {get;set; }
public int ID { get;set; }
}
<input type='text' class='input-choice' id='txtChoice0' name='Department[0].Name' />
Now my question is how should I apply validation to this. I have marked Required Attribute over the department collection in the Employee class and as well as on the field level in Department object, but nothing works.
Model binding works perfectly fine, so I do see that the name 'DepartmentList[0].Name' in the input field maps correctly to the collection property in the Employee object in the controller and then I thought same way my validations will work but they don't
get fired at all
Since validation doesn't recurse on the model, consider having your top model (Employee in this case) implement
IValidatableObject. It can then check the validation of its child objects and report it back up to MVC.
Isn't this part of .NET framwork 4.5 . I am using .NET version 4.0 and MVC 2 ??
Collection is not the problem!!! Client side validation needs html5 attributes that translates the conditions of the validation attributes. Such validation attributes are added by the html helpers like Html.TextboxFor...if you write simply <input......no attributes
are created and clint side validation doesnt work.
Collection is not the problem!!! Client side validation needs html5 attributes that translates the conditions of the validation attributes. Such validation attributes are added by the html helpers like Html.TextboxFor...if you write simply <input......no attributes
are created and clint side validation doesnt work.
Regardless if you have client side validation, he will always need server side validation and for nested object model binding the validation attributes on nested objects aren't honored.
Server side validation is pplied by the default model binder and since the drfault model binder is recursive it works ALSO for nested objects...hohrver all errors names are prefixed eith thr whole path ftom tje root model to the leaf property in error...if
...errors are not shown is becsusr thr right name convrntion has not been respected
kunal1982
Member
42 Points
52 Posts
How to apply validation to a collection item in Asp.net MVC 2
May 04, 2012 07:09 PM|LINK
I have a strongly typed view, which has some input boxes to map to a collection in an entity. Say for an example , take case of a View for adding an Employee detail and within that there are input fields to enter department name as well (say 2). Both of them are required.
Here is the class structure of these two entities:
public class Employee { public int EmployeeID{get;set;} public string Name {get;set; } public IList<Department> DepartmentList{get;set;} } public class Deparment { public string Name {get;set; } public int ID { get;set; } } <input type='text' class='input-choice' id='txtChoice0' name='Department[0].Name' />Now my question is how should I apply validation to this. I have marked Required Attribute over the department collection in the Employee class and as well as on the field level in Department object, but nothing works.
Any ideas ??
mvc MVC2
Kunal Uppal
Software Engineer,
ICS, Mumbai
JohnLocke
Contributor
3160 Points
702 Posts
Re: How to apply validation to a collection item in Asp.net MVC 2
May 04, 2012 08:11 PM|LINK
Have you tried adding data annotations to the Department class' properties?
[Required]
public string Name { get;set }
Then you can tuck your code inside an if (ModelState.IsValid) { ... } conditional
Just remember that it validates against the model you pass to your ActionResult.
kunal1982
Member
42 Points
52 Posts
Re: How to apply validation to a collection item in Asp.net MVC 2
May 04, 2012 08:20 PM|LINK
Yes that is already done, but client side validation fails to run...
Also, my Markup for the input field and the associated validation is like this :
<input type='text' class='input-choice' id='txtChoice0' name='DepartmentList[0].Name' /> <%=Html.ValidationMessage("DepartmentList[0].Name") %>
Model binding works perfectly fine, so I do see that the name 'DepartmentList[0].Name' in the input field maps correctly to the collection property in the Employee object in the controller and then I thought same way my validations will work but they don't get fired at all
Kunal Uppal
Software Engineer,
ICS, Mumbai
BrockAllen
All-Star
27512 Points
4895 Posts
MVP
Re: How to apply validation to a collection item in Asp.net MVC 2
May 04, 2012 09:21 PM|LINK
Edit -- I was incorrect in my original statement.
You can still implemenet implement IValidatableObject for custom validation in your model.
DevelopMentor | http://www.develop.com
thinktecture | http://www.thinktecture.com/
kunal1982
Member
42 Points
52 Posts
Re: How to apply validation to a collection item in Asp.net MVC 2
May 04, 2012 09:26 PM|LINK
Isn't this part of .NET framwork 4.5 . I am using .NET version 4.0 and MVC 2 ??
Kunal Uppal
Software Engineer,
ICS, Mumbai
BrockAllen
All-Star
27512 Points
4895 Posts
MVP
Re: How to apply validation to a collection item in Asp.net MVC 2
May 04, 2012 09:29 PM|LINK
It's in .NET 4. If you're still in MVC2/.NET3.5, then you can implement IDataErrorInfo.
DevelopMentor | http://www.develop.com
thinktecture | http://www.thinktecture.com/
francesco ab...
All-Star
20910 Points
3278 Posts
Re: How to apply validation to a collection item in Asp.net MVC 2
May 04, 2012 11:07 PM|LINK
Mvc Controls Toolkit | Data Moving Plug-in Videos
BrockAllen
All-Star
27512 Points
4895 Posts
MVP
Re: How to apply validation to a collection item in Asp.net MVC 2
May 04, 2012 11:12 PM|LINK
Regardless if you have client side validation, he will always need server side validation and for nested object model binding the validation attributes on nested objects aren't honored.
DevelopMentor | http://www.develop.com
thinktecture | http://www.thinktecture.com/
francesco ab...
All-Star
20910 Points
3278 Posts
Re: How to apply validation to a collection item in Asp.net MVC 2
May 04, 2012 11:25 PM|LINK
Mvc Controls Toolkit | Data Moving Plug-in Videos
BrockAllen
All-Star
27512 Points
4895 Posts
MVP
Re: How to apply validation to a collection item in Asp.net MVC 2
May 04, 2012 11:27 PM|LINK
I'll double check but I've not found this to be the case in my testing.
DevelopMentor | http://www.develop.com
thinktecture | http://www.thinktecture.com/