Last post Feb 19, 2009 11:00 AM by Luis Abreu
Feb 18, 2009 05:00 PM|Nyla Pareska|LINK
What does this do??
I found this sample:
http://www.asp.net/learn/mvc/tutorial-21-cs.aspx. Very good but I do not understand the meaning of <%= Html.ValidationMessage("Title", "*") %>. When in edit mode and fill in nothing then only for date I see some red marking that there is something wrong
for my input.
But the messages says A value is required. Does this mean that it is something like requiredfield validator from normal asp.net? If so I expect also the same logic for the title and director fields but this does not show anything.
Can enyone explain this to me?
Feb 18, 2009 05:33 PM|Divi|LINK
The UpdateModel<T> method is a helper which searches all property values of your object from the request object. If it finds an index in the request object which is named as a property of your object, it will try to fill in the value into the property of
your object. If this doesn't work, it will call ViewData.ModelState.AddModelError("PROPERTY_NAME", "VALIDATION_MESSAGE").
If you now add a ValidationMessage, it will check the ViewData.ModelState for a property with the name PROPERTY_NAME and check wheter an error has occured. So you have a nicely seperated validation info from a model which may be in faulted state. If an error
occured it will display the validation message (in your case PROPERTY_NAME is "Title" and the validation message is "*").
Feb 18, 2009 09:24 PM|Luis Abreu|LINK
Hello. I've written some posts about validation in mvc in the past. Probably they might help you getting started:
Feb 19, 2009 08:36 AM|Nyla Pareska|LINK
Feb 19, 2009 10:34 AM|Luis Abreu|LINK
ValidationMessage is there just so that you can give some feedback regarding an error that exists in a control. Ie, it's just a holder for showing a specific error. validation should be performed by your controller (or a specific object, if it implements
the IDataErrorInfo interface) and all errors should be appended to the ViewData ModelState property.
Feb 19, 2009 10:57 AM|Nyla Pareska|LINK
Feb 19, 2009 11:00 AM|Luis Abreu|LINK
probably something like this: