Good day! There is a form in which you need to enter a name. If you enter more than 3 (restriction as in the example) characters, an error message + the same form appears. Total error message and two identical forms. Please tell me how to cure it?

A little updated ..

You can change the controller and add the following

if (Request.IsAjaxRequest()) { return PartialView("NameError"); } 

But such a solution completely eats @ Html.ValidationSummary () and simply displays a partial view with an error, which is wildly not convenient if you need to enter two entries in the form, and if there are 5 of them .. you will not enter answers to all possible errors in the view + When the message is displayed, the form remains, but in this example it is rather a plus. So I still don’t understand if you don’t use NameError, why when @ Html.ValidationSummary () duplicates the form, and if successful, doesn’t replace the form with a message? What did I miss?

Controller

 [HttpPost] public ActionResult Name(Name name) { if (ModelState.IsValid) { repository.SaveName(name); if (Request.IsAjaxRequest()) { return PartialView("NameSent"); } return View("NameSent"); } return View(); } 

Name view

 @{ AjaxOptions ajaxName = new AjaxOptions { UpdateTargetId = "target", HttpMethod = "post" }; } <div> @Html.ValidationSummary() <p id="target"></p> </div> <div class=""> @using (Ajax.BeginForm("Name", ajaxName)) { @Html.TextBoxFor(m => m.Name) <div class=""> <input type="submit" value="Сохранить" class="btn btn-primary" /> </div> } </div> 

Model

 public class Name { public int NameId { get; set; } [Required(ErrorMessage = "Укажите ваше имя")] [StringLength(3)] /*Просто для примера*/ public string Name { get; set; } } 
  • Is jquery-unobtrusive-ajax.js connected twice on the page? What does the "NameSent" partial view look like? - Basilevs
  • jquery-unobtrusive-ajax.js - connected once. NameSent looks very simple (<h2> Record added </ h2>). - waines
  • or do it altogether so return Content ("Record added"); but the problem remains. - waines

0