title: How to show validation message in mvc4 razor
body: <p>I am newbie in MVC Razor and I want to implement validation message on textboxes. Here I'm creating some textbox dynamically as follows:</p>
  
  <p><strong>View Code:</strong></p>
  
  <pre><code>foreach (var items in (IEnumerable&lt;System.Data.DataRow&gt;)Model.UsersOfList)
  {
  
    @Html.TextBoxFor(m =&gt; m.LoginNameOfLoginInfoTab, new { @class = "textBox_LoginInfoAndPermission", @value = (Model.LoginNameOfLoginInfoTab = items["UserName"].ToString()), @id = ("txtUserLoginName" + Model.UsernameOfLoginInfoTab.Trim()) })
  
    @Html.ValidationMessageFor(m =&gt; m.LoginNameOfLoginInfoTab, null, new { @class = "ErrorMessage" })
  
    @Html.TextBoxFor(m =&gt; m.UsernameOfLoginInfoTab, new { @class = "textBox_LoginInfoAndPermission", @value = (Model.UsernameOfLoginInfoTab = items["FirstName"].ToString()), @id = ("txtUserName" + Model.UsernameOfLoginInfoTab.Trim()) })
  
    @Html.ValidationMessageFor(m =&gt; m.UsernameOfLoginInfoTab, null, new { @class = "ErrorMessage" })
  
  
  }
  </code></pre>
  
  <p>in the module I've written code for validation as follows:</p>
  
  <pre><code>[Required (ErrorMessage="*")]
      public string UsernameOfLoginInfoTab
      {
          get;
          set;
      }
  
  
     [Required(ErrorMessage = "*")]
      public string LoginNameOfLoginInfoTab
      {
          get;
          set;
      }
  </code></pre>
  
  <p>Now when all textboxes has been created and when one validation message is displaying for first loop iteration textbox then it will automatically displaying in front of another textbox too which is created on second loop iteration. </p>
  
  <p>Please tell me whats going wrong. </p>