title: Display Error message in Validationsummary with Ajax
body: <p>Goal:<br>
  If you retrieve a error based on the input, it should be displayed in ValidationSummary in relation to ajax without the webpage being refreshed.</p>
  
  <p>Problem:<br>
  I have tried to make it but it doesn't work so well.  </p>
  
  <p>What part am I missing?  </p>
  
  <p>Thank you!  </p>
  
  <p>Info:<br>
  *I have found some webpages but they do not fit exactly to my purpose.<br>
  *I'm using ASP.net mvc</p>
  
  <pre><code>@model WebApplication1.Controllers.PersonnelModel
  
  @{
      ViewBag.Title = "Ajax";
  }
  
  &lt;h2&gt;Ajax&lt;/h2&gt;
  
  
  
  &lt;h2&gt;AjaxViewModel&lt;/h2&gt;
  @using (Html.BeginForm("HtmlViewModel", "Home", null))
  {
      @Html.ValidationSummary(true)
  
      &lt;fieldset&gt;
          &lt;legend&gt;PersonnelModel&lt;/legend&gt;
          &lt;div class="editor-label"&gt;
              @Html.LabelFor(model =&gt; model.UserName)
          &lt;/div&gt;
          &lt;div class="editor-field"&gt;
              @Html.EditorFor(model =&gt; model.UserName)
              @Html.ValidationMessageFor(model =&gt; model.UserName)
          &lt;/div&gt;
          &lt;div class="editor-label"&gt;
              @Html.LabelFor(model =&gt; model.MailAdress)
          &lt;/div&gt;
          &lt;div class="editor-field"&gt;
              @Html.EditorFor(model =&gt; model.MailAdress)
              @Html.ValidationMessageFor(model =&gt; model.MailAdress)
          &lt;/div&gt;
      &lt;/fieldset&gt;
      &lt;p&gt;
          &lt;input type="submit" value="Html Form Action" /&gt;
      &lt;/p&gt;
  }
  
  &lt;br/&gt;
  &lt;br /&gt;
  
  
  
  
  &lt;h2&gt;AjaxViewModel&lt;/h2&gt;
  @using (Ajax.BeginForm("AjaxViewModel", "Home", new AjaxOptions { UpdateTargetId = "result" }))
  {
      @Html.ValidationSummary(true)
      &lt;fieldset&gt;
          &lt;legend&gt;PersonnelModel&lt;/legend&gt;
  
          &lt;div id="result"&gt;&lt;/div&gt;
          &lt;div class="editor-label"&gt;
              @Html.LabelFor(model =&gt; model.UserName)
          &lt;/div&gt;
          &lt;div class="editor-field"&gt;
              @Html.EditorFor(model =&gt; model.UserName)
              @Html.ValidationMessageFor(model =&gt; model.UserName)
          &lt;/div&gt;
  
          &lt;div class="editor-label"&gt;
              @Html.LabelFor(model =&gt; model.MailAdress)
          &lt;/div&gt;
          &lt;div class="editor-field"&gt;
              @Html.EditorFor(model =&gt; model.MailAdress)
              @Html.ValidationMessageFor(model =&gt; model.MailAdress)
          &lt;/div&gt;
      &lt;/fieldset&gt;
      &lt;p&gt;
          &lt;input type="submit" value="Ajax Form Action" /&gt;
      &lt;/p&gt;
  }
  
  
  
  &lt;script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.8.3/jquery.min.js"&gt;&lt;/script&gt;
  &lt;script src="https://cdn.jsdelivr.net/npm/jquery-ajax-unobtrusive@3.2.4/jquery.unobtrusive-ajax.min.js"&gt;&lt;/script&gt;
  </code></pre>
  
  <hr>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.ComponentModel.DataAnnotations;
  using System.Linq;
  using System.Threading.Tasks;
  using System.Web;
  using System.Web.Mvc;
  using System.Web.Security;
  
  namespace WebApplication1.Controllers
  {
      public class HomeController : Controller
      {
          public ActionResult Index()
          {
              return View();
          }
  
  
          [HttpPost]
          public ActionResult HtmlViewModel(PersonnelModel Pmodel)
          {
              return Content("Hi " + Pmodel.UserName + ", Thanks for the details, a mail will be sent to " + Pmodel.MailAdress + " with all the login details.", "text/html");
          }
  
  
          [HttpPost]
          public ActionResult AjaxViewModel(PersonnelModel Pmodel)
          {
              /*
              ModelState.AddModelError("", "login is fail");
              return View("Index", Pmodel);
              */
  
              return Content("Hi " + Pmodel.UserName + ", Thanks for the details, a mail will be sent to " + Pmodel.MailAdress + " with all the login details.", "text/html");
          }
  
      }
  
  
  
      public class PersonnelModel
      {
          [Required(ErrorMessage = "UserName Required.")]
          public string UserName { get; set; }
  
          [Required(ErrorMessage = "Email id Required.")]
          public string MailAdress { get; set; }
      }
  
  }
  </code></pre>