MVC - 将EditorTemplate与EditorForModel一起使用时,模型验证不会触发

时间:2017-05-25 14:39:12

标签: asp.net-mvc mvc-editor-templates html.beginform

我正在EditorTemplate使用Html.EditorForModel() 在没有EditorTemplate的情况下使用时,验证有效。 没有EditorTemplate - 验证不起作用。

这是我的表格:

<div style="padding-top: 20px;">
   @using (Html.BeginForm())
   {
        @Html.EditorForModel()
   }
</div>

这是EditorTemplate:

<div style="margin: 0px auto; text-align: center; display: table;">
    <table>
        <tr>
            <td>@Html.EditorFor(x => x.UserName, new { htmlAttributes = new { placeholder = "username" } })</td>
        </tr>
        <tr>
            <td>@Html.EditorFor(x => x.Password, new { htmlAttributes = new { placeholder = "password" } })</td>
        </tr>
        <tr style="height:70px;">
            <td><input type="submit" class="k-primary" value="Log In" /></td>
        </tr>

    </table>
</div>

这是html生成的:enter image description here

我错过了什么?

1 个答案:

答案 0 :(得分:1)

我通过为两个属性添加Html.ValidationMessageFor来解决它

@Html.ValidationMessageFor(x => x.UserName,"", new { @class = "text-danger"})