I'll double check but I've not found this to be the case in my testing.
Ah yes, my mistake... my test was flawed.
So back to the original poster, you can then easily apply the validation attributes to the nested class (as well as implement IDataErrorInfo for further validation as needed).
It appears that also the more expert people have doubts on the exact way name convention are used in model binding and in error handling exactly....probably because there is pratically no documentation on the subject... I will write asap a blog post about
name conventions and prefix handling in my blog(there are already 2 posts scheduled, so maybe it will apeear in about 1 month).
No, nested model binding I am fine with -- I just made a mistake on my test. I had never tried to put validation attributes on the nested classes to see if validation also flowed (or I had forgotten).
So I used this test code:
public class Baz
{
[Required]
public string Quux { get; set; }
}
public class Foo
{
[Required]
public string Bar { get; set; }
public Baz Baz { get; set; }
}
public class HomeController : Controller
{
public ActionResult Index(Foo foo)
{
var v = ModelState.IsValid;
return View();
}
}
public class Baz
{
[Required]
public string Quux { get; set; }
}
public class Foo
{
[Required]
public string Bar { get; set; }
[Required]
public Baz Baz { get; set; }
}
public class HomeController : Controller
{
public ActionResult Index(Foo foo)
{
var v = ModelState.IsValid;
return View();
}
}
No, nested model binding I am fine with -- I just made a mistake on my test. I had never tried to put validation attributes on the nested classes to see if validation also flowed (or I had forgotten).
No doubt you know the name conventions for nested model binding. In general, after one starts playing with more complex models or with lists...one master these rules.
HOWEVER, there a lot of small ..things related to them, that in some circumstances becomes very important, such as:
The way prefixes are handled by several parts of the system. As a default a lot of routines adds prefixes to the names...some Rendering helpers do it some others...do not. Errors handling routines also ...handles automatically prefixes...most of the times...:)
Smart ways to "adjust" prefixes(smart means...while continuing to use the ...TextBoxFor..and similar helpers) prefixes when the action method where we post has a model that is different from the rendering model.
What happens exactly when an action method has several parameters that are complex objects ...in this cases the names of the parameters may cause problems...unluckly such problems appears also when we have just one parameter...and we don't care its name
because we want to bind simply the parameter properties.
Does the TextBox and TextBoxFor do the same "stuff"? I mean if we use string expressions instead of lambda expressions can we substitute TextBoxFor with TextBox ? This is usefull when we have to post to a different model....unlukly the general answer is
NO!!! We cant substitute them...the risk is that some metadata are not computed with TextBox...and similar...because the Metadata retrieving functions for lambda and string expressions works differently....If the model is not nested they are equivalent...but
in other cases they are not
How does validation summary and ValidationMessageFor use nested expression string in determinin their behaviour?...
When we define a custom Validation attribute...like the CompareAttribute that depends also on another property..are there limitations to the "other property" in case of nested models?....UNLUCLY THERE ARE LIMITATIONS....that however can be overcome with
some tricks....
How to handle collections with multiple indices(multidimensional arrays)?
.......And the list continue...
Does everyone has an answer to all aboveproblems? (and to several other name convention related problems)
I discovered all this problems while implementing "tools" that forced me to study the sources....However I noticed that most of people don't know such stuffs probably because most of the above problems are nopt clearly stated in some place...(there is something
in this forum...and in stackoverflow...).....however when the problem is "hit" they go crazy because some "stuffs" are not so easy to imagine
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
Honestly , I am more confused now. So do you mean the problem is with the naming convention of the validation helper that I have used.
??
I am using <%=Html.ValidationMessage("Department[0].Name") %> after the first input. If I also pass on the error message along with the model name , error message appears right away on the view load
Model binding on other hand is working perfectly fine, I get both Parent and its child(collection of child in fact) in the controller.
Also , I thought MVC will have a very straighforward approach for applying validation to cotrols representing child collection. My only requirement is that I need to add Parent and child together in a single view.
I do not have much exp in MVC so don't know if my following the right approach or not.
So do you mean the problem is with the naming convention of the validation helper that I have used.
No my answer was for Brock that asserted that the problkem was the nameing convention. However, my general observation apply also to your problem. "Most of people have not very clear the role of naming conventions". In your case the name convention is ok.
However the name conventione ALONE is not enough for the client side validation to work.
You used ValidationMessage to apply error but this is not enough. ValidationMassage is just a container where to write the error. The actual error is handled by the input field. Now you wrote simply <input....that is you have not used the Html helpers for
that field. Also if you apply the right name convention this is not enough for client side valdation to work. In fact when you call TextBox or TextBoxFor they creat an <input....with the right name convention AND ALSO add them some Html5 data- .... attributes
with informations about the client side validation rule to apply. If you want to write <input....manually you MUST write yourself MANUALLY this information. I hope now everything is clear VALIDATION ERROR INFORMATION ARE NOT STORED IN ValidationMessage BUT
IN THE INPUT FIELDS.
BrockAllen
All-Star
28084 Points
4997 Posts
MVP
Re: How to apply validation to a collection item in Asp.net MVC 2
May 04, 2012 11:30 PM|LINK
Ah yes, my mistake... my test was flawed.
So back to the original poster, you can then easily apply the validation attributes to the nested class (as well as implement IDataErrorInfo for further validation as needed).
DevelopMentor | http://www.develop.com
thinktecture | http://www.thinktecture.com/
francesco ab...
All-Star
20944 Points
3286 Posts
Re: How to apply validation to a collection item in Asp.net MVC 2
May 05, 2012 08:54 AM|LINK
It appears that also the more expert people have doubts on the exact way name convention are used in model binding and in error handling exactly....probably because there is pratically no documentation on the subject... I will write asap a blog post about name conventions and prefix handling in my blog(there are already 2 posts scheduled, so maybe it will apeear in about 1 month).
Mvc Controls Toolkit | Data Moving Plug-in Videos
BrockAllen
All-Star
28084 Points
4997 Posts
MVP
Re: How to apply validation to a collection item in Asp.net MVC 2
May 05, 2012 01:42 PM|LINK
No, nested model binding I am fine with -- I just made a mistake on my test. I had never tried to put validation attributes on the nested classes to see if validation also flowed (or I had forgotten).
So I used this test code:
public class Baz { [Required] public string Quux { get; set; } } public class Foo { [Required] public string Bar { get; set; } public Baz Baz { get; set; } } public class HomeController : Controller { public ActionResult Index(Foo foo) { var v = ModelState.IsValid; return View(); } }And when I tested I used:
/Home/Index?foo.Bar=1&foo.Baz.Quux=2 // IsValid == true
/Home/Index?foo.Bar=1 // IsValid == true
The 2nd query string is where I was mislead -- I should have used:
/Home/Index?foo.Bar=1&foo.Baz.Quux= // IsValid == false
And then I could have also changed the code:
public class Baz { [Required] public string Quux { get; set; } } public class Foo { [Required] public string Bar { get; set; } [Required] public Baz Baz { get; set; } } public class HomeController : Controller { public ActionResult Index(Foo foo) { var v = ModelState.IsValid; return View(); } }And now with this change:
/Home/Index?foo.Bar=1 // IsValid == false
So yea, I was hasty in my test :/
DevelopMentor | http://www.develop.com
thinktecture | http://www.thinktecture.com/
francesco ab...
All-Star
20944 Points
3286 Posts
Re: How to apply validation to a collection item in Asp.net MVC 2
May 05, 2012 07:14 PM|LINK
No doubt you know the name conventions for nested model binding. In general, after one starts playing with more complex models or with lists...one master these rules.
HOWEVER, there a lot of small ..things related to them, that in some circumstances becomes very important, such as:
.......And the list continue...
Does everyone has an answer to all aboveproblems? (and to several other name convention related problems)
I discovered all this problems while implementing "tools" that forced me to study the sources....However I noticed that most of people don't know such stuffs probably because most of the above problems are nopt clearly stated in some place...(there is something in this forum...and in stackoverflow...).....however when the problem is "hit" they go crazy because some "stuffs" are not so easy to imagine
Mvc Controls Toolkit | Data Moving Plug-in Videos
kunal1982
Member
42 Points
52 Posts
Re: How to apply validation to a collection item in Asp.net MVC 2
May 05, 2012 09:27 PM|LINK
Honestly , I am more confused now. So do you mean the problem is with the naming convention of the validation helper that I have used.
??
I am using <%=Html.ValidationMessage("Department[0].Name") %> after the first input. If I also pass on the error message along with the model name , error message appears right away on the view load
Model binding on other hand is working perfectly fine, I get both Parent and its child(collection of child in fact) in the controller.
Also , I thought MVC will have a very straighforward approach for applying validation to cotrols representing child collection. My only requirement is that I need to add Parent and child together in a single view.
I do not have much exp in MVC so don't know if my following the right approach or not.
Kunal Uppal
Software Engineer,
ICS, Mumbai
francesco ab...
All-Star
20944 Points
3286 Posts
Re: How to apply validation to a collection item in Asp.net MVC 2
May 06, 2012 07:11 AM|LINK
No my answer was for Brock that asserted that the problkem was the nameing convention. However, my general observation apply also to your problem. "Most of people have not very clear the role of naming conventions". In your case the name convention is ok. However the name conventione ALONE is not enough for the client side validation to work.
You used ValidationMessage to apply error but this is not enough. ValidationMassage is just a container where to write the error. The actual error is handled by the input field. Now you wrote simply <input....that is you have not used the Html helpers for that field. Also if you apply the right name convention this is not enough for client side valdation to work. In fact when you call TextBox or TextBoxFor they creat an <input....with the right name convention AND ALSO add them some Html5 data- .... attributes with informations about the client side validation rule to apply. If you want to write <input....manually you MUST write yourself MANUALLY this information. I hope now everything is clear VALIDATION ERROR INFORMATION ARE NOT STORED IN ValidationMessage BUT IN THE INPUT FIELDS.
Mvc Controls Toolkit | Data Moving Plug-in Videos