TagHelper validation when editing a list

我只是一个虾纸丫 提交于 2019-12-11 10:18:21

问题


I am trying to use TagHelpers when editing a list of items.

This is the code inside the view where helpers are employed:

@for (var i = 0; i < Model.Items.Count; i++)
        {
            <tr>
                <td>
                    <input asp-for="Items[i].Name" />
                    <span asp-validation-for="Items[i].Name" class="text-danger" />
                </td>
            </tr>
        }

Input helpers are working as expected, ModelState is properly filled with validation errors but validation errors are not shown to the user.

I'm guessing there's a problem with rendering validation tags.

<td>
                    <input class="input-validation-error" data-val="true" data-val-required="The Name field is required." id="Items_0__Name" name="Items[0].Name" value="" type="text">
                    <span class="text-danger field-validation-error" data-valmsg-for="Items[0].Name" data-valmsg-replace="true"></span>
</td>

It could be that input Id is Items_0__Name (with underscores) but validation tag looks for Items[0].Name.

Is there a workaround to make the validation work with this?


回答1:


I have never seen any problems using a foreach

@foreach (var item in Model.Items)
{
    <tr>
        <td>
            <input asp-for="item.Name" />
            <span asp-validation-for="item.Name" class="text-danger" />
        </td>
    </tr>
}


来源:https://stackoverflow.com/questions/34732092/taghelper-validation-when-editing-a-list

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!