0

I have a "Contact Us" page where I have a list of questions and answers like so:

public List<string> Questions = new List<string>()
                                    {
                                        "What browser are you using?",
                                        "What version of the browser are you using?",
                                        "And so on and so forth..."
                                    };

public List<string> Answers { get; set; }

Then I'm adding these to the view like so:

           @foreach (var question in Model.Questions)
            {
                <dt>
                    <dd width="240" height="25">@Html.Label(question)</dd>

                    <dd width="240" height="80">
                        @Html.ValidationMessageFor(model => model.Answers[Model.Index])
                        @Html.TextAreaFor(model => model.Answers[Model.Index], new { @class = "uniform", @id = "text" })
                      </dd>
                </dt>
                Model.Index += 1;
            }

Before I go about writing a custom validator, is there a way I can validate each string in that list of answers similar to the way I would validate a string field?:

[StringLength(100)]

Thanks in advance!!

3
  • what are you trying to validate? Commented Nov 22, 2011 at 1:59
  • I am not sure it's fair for you to be so critical - if you look at my history... whenever I ask a question that has not been answered, or the answer is incorrect, I have followed up by answering my own question for the benefit of the community. Commented Nov 22, 2011 at 15:50
  • @Dallas - I am trying to validate the length of the responses to ensure they are not entering a value that is too large. Commented Nov 22, 2011 at 15:53

1 Answer 1

2

Your list of questions should be another model contained within your Contact Us page model so you can then assign those attributes to that model. This way you'll get the validation on each item.

Another alternative is simply to check the length in the controller code (no client validation here of course) and use ModelState.AddError() to manually add an error if your validation fails.

A third option (server side again) is to implement IValidateableObject in your model to manually check these values.

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

1 Comment

I did realize those second two options, but I am trying to use the existing client side functionality before I bother to reinvent the wheel. I am on a team of 20 developers so I don't want to introduce added confusion. I'm unclear on that first option as to how I would move my list of answers / questions to another model. Are you suggesting that I simply don't use a List? I did try creating a new class (and changed the corresponding loop in the view) that held a string field and the [StringLength] attribute set to [StringLength(100)]...but validation did not work in this scenario.

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.