3

I have a questionnaire that consists of multiple pages of 12 statements, for each of which the user must specify the degree to which the statement matches them. looks like this:

questionnaire

My view model looks like this:

public class PageViewModel
{
    public List<ItemViewModel> Items { get; set; }
}

public class ItemViewModel
{
    public Guid Id { get; set; }
    public int Number { get; set; }
    public string Text { get; set; }

    [Required]
    public int Response { get; set; }
}

And my view:

@model PageViewModel

@using(Html.BeginForm()){
<table>
    <tr>
        <td></td>
        <td></td>
        <th>1</th>
        <th>2</th>
        <th>3</th>
        <th>4</th>
        <th>5</th>
    </tr>

    @Html.EditorFor(model => model.Items)

    @Html.ValidationSummary(false)

</table>

<input type="submit"/>
}

and the editor template tahts repeated for each item:

@model ItemViewModel

<tr>
    <td>@Model.Number:</td>
    <td>@Model.Text</td>

    @for(int i = 1; i <= 5; i++){
        <td>@Html.RadioButtonFor(model => model.Response, i)</td>
    }
<tr>

I would like it if the validation summary would list the statements for which a response is required, eg. "A response is required for statement 6". Currently it is just repeating "The response field is required"

I should imagine a custom validator is required, but writing one of my own from scratch is a bit beyond me at the moment, so any help would be appreciated.

Ben
  • 5,525
  • 8
  • 42
  • 66

1 Answers1

0

You' ll need to write your own Custom Validation Summary Template.

namespace System.Web.Mvc
{
    public static class ViewExtensions
    {
        public static string MyValidationSummary(this HtmlHelper html, string validationMessage)
        {
            if (!html.ViewData.ModelState.IsValid)
            {
                return "customized message from here";
            }

            return "";
        }
    }
}

Also see this - How to extend the ValidationSummary HTML Helper in ASP.NET MVC?

Community
  • 1
  • 1
Yasser Shaikh
  • 46,934
  • 46
  • 204
  • 281