3

12 のステートメントの複数ページで構成されるアンケートがあります。ユーザーは、ステートメントが一致する度合いをそれぞれのステートメントに対して指定する必要があります。次のようになります。

アンケート

私のビューモデルは次のようになります。

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; }
}

そして私の見解:

@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"/>
}

そして、アイテムごとに繰り返されるエディタ テンプレート:

@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>

検証の要約に、応答が必要なステートメントがリストされていることを望みます。「ステートメント 6 には応答が必要です」。現在、「応答フィールドが必要です」を繰り返すだけです

カスタムバリデータが必要だと想像する必要がありますが、現時点では自分のものをゼロから作成することは少し難しいので、助けていただければ幸いです。

4

1 に答える 1

0

独自の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 "";
        }
    }
}

これも参照してください - ASP.NET MVC で ValidationSummary HTML ヘルパーを拡張する方法は?

于 2014-03-03T11:21:22.317 に答える