2

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?

1
  • Is the dynamic part of the form generated client side or server side? Commented Dec 5, 2008 at 22:23

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.