tags:

views:

112

answers:

2

Scenario: A user enters a string in the DateTime field. A * appears when a postback occurs but no message in a ValidationSummary. I have tried implementing IDataErrorInfo but the code never falls through IDataErrorInfo.this[string columnName] for the EventDate field. I have tried implementing DataAnnotations attributes again this doesn't work. By the time code reaches the attribute checks the EventDate has been changed to DateTime.MinValue by the framework.

Any help gratefully received.

I have posted the same questions to ASP.Net MVC forums, Steve Sanderson, Stephen Walter and Schotime but no answers so whoever fixes this is a MVC Master.

View:

<%= Html.ValidationSummary() %>
<label for="EventDate">EventDate:</label>
<%=Html.TextBox("Dinner", Model.EventDate)%>
<%= Html.ValidationMessage("EventDate", "*") %>

Controller:

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Edit(Dinner item)
public ActionResult Edit(int ID, FormCollection coll)
{
    //What would you recommend?
}
A: 

If you're using the first method signature that accepts a Dinner object and you add change your textbox to reflect the property name...

<%= Html.TextBox("Dinner.EventDate", Model.EventDate) %>

and your validation message...

<%= Html.ValidationMessage("Dinner.EventDate", "*") %>

It should then work.

Chad Moran
A: 

The answer comes in the form of a new Data Annotations Model Binder Sample provided by Microsoft at http://aspnet.codeplex.com/Release/ProjectReleases.aspx?ReleaseId=24471.

This will now show invalid values in a validation summary.

Hopefully this will make it into a ASP.Net MVC 1.01 release.

The error message is hardcoded into the DLL so I have suggested creating a new ValidationAttribute where you can specify your own custom message. Read more here. http://forums.asp.net/t/1406636.aspx

Jon