2017-05-25 60 views
0

我使用EditorTemplate对于Html.EditorForModel() 当使用没有EditorTemplate时,验证有效。 没有EditorTemplate - 验证不起作用。MVC - 使用EditorTemplate和EditorForModel时,模型验证不会触发

这是我的方式:

<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

我缺少什么?

+0

出于纯粹的好奇,有没有您使用的是EditorForModel与EditorTemplate的理由,而不是直接插入EditorTemplate的内容在形式? – sleeyuen

+0

没有理由。只是为了自我教育的目的:) – gene

+0

好,够公平! – sleeyuen

回答

1

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

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