2017-05-25 63 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"})