views:

927

answers:

1

I'm building an ASP.NET MVC application with a form that needs validation. The majority of the form is static, but part of the form is dynamic.

I need to enable the user to enter n string/date combinations.

The string/date combos need to be validated server side, and I need to give feedback to the user preferably directly beside the combination that failed validation.

For static input I do the following:

   <%= Html.ValidationMessage("someField") %>

For the dynamic data, what should I do?

+2  A: 

In your controller you'll want to assign an error to the particular fields that fail validation:

ModelState.AddModelError ("textbox1", "You must specify a valid string.");
ModelState.AddModelError ("combobox1", "You must specify a valid date.");

Then all the helper is really doing is checking if the following exists:

ViewData.ModelState.ContainsKey("textbox1")

and then creating a tag such as follows

<span><%= ViewData.ModelState.ContainsKey("textbox1").Errors[0] %></span>

the helper does a bit more null value checking but you get the idea.

Todd Smith