Validation does not work when using AJAX request [duplicate]












0
















This question already has an answer here:




  • jQuery Validate SubmitHandler with Ajax Submission Not being hit

    1 answer



  • Jquery post and unobtrusive ajax validation not working mvc 4

    1 answer




I'm trying to validate fields with ValidationMessageFor, and everything works fine if you submit the form using Html.Beginform, but if I use an AJAX query, then validations no longer work.
My Code
View:



@model AutoStore.Domain.Core.Client

<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css" />

<style>
html,
body {
height: 100%;
}

body {
display: -ms-flexbox;
display: flex;
-ms-flex-align: center;
align-items: center;
padding-top: 40px;
padding-bottom: 40px;
}

.form-signin {
width: 100%;
max-width: 330px;
padding: 15px;
margin: auto;
}

.input-group-text {
border-top-left-radius: 30px;
border-bottom-left-radius: 30px;
}

.form-control {
border-radius: 30px;
}
</style>

@Html.ActionLink("Главная", "Index")

@using (Html.BeginForm())
{
<div class="container">
<div class="form-signin">
<div class="form-group">
@Html.LabelFor(i => i.CSurname, "Фамилия")
<div class="cols-sm-10">
<div class="input-group">
<span class="input-group-text"><i class="fa fa-id-badge" aria-hidden="true"></i></span>
@Html.EditorFor(i => i.CSurname, new { htmlAttributes = new { @id = "txtSurname", @class = "form-control", @placeholder = "Введите фамилию" } })
</div>
@Html.ValidationMessageFor(i => i.CSurname, "", new { @class = "text-danger" })
</div>
</div>

<div class="form-group">
@Html.LabelFor(i => i.CName, "Имя")
<div class="cols-sm-10">
<div class="input-group">
<span class="input-group-text"><i class="fa fa-id-badge" aria-hidden="true"></i></span>
@Html.EditorFor(i => i.CName, new { htmlAttributes = new { @id = "txtName", @class = "form-control", @placeholder = "Введите имя" } })
</div>
@Html.ValidationMessageFor(i => i.CName, "", new { @class = "text-danger" })
</div>
</div>

<div class="form-group">
@Html.LabelFor(i => i.CPatronymic, "Отчество")
<div class="cols-sm-10">
<div class="input-group">
<span class="input-group-text"><i class="fa fa-id-badge" aria-hidden="true"></i></span>
@Html.EditorFor(i => i.CPatronymic, new { htmlAttributes = new { @id = "txtPatr", @class = "form-control", @placeholder = "Введите отчество" } })
</div>
@Html.ValidationMessageFor(i => i.CPatronymic, "", new { @class = "text-danger" })
</div>
</div>

<div class="form-group">
@Html.LabelFor(i => i.Login, "Логин")
<div class="cols-sm-10">
<div class="input-group">
<span class="input-group-text"><i class="fa fa-user fa" aria-hidden="true"></i></span>
@Html.EditorFor(i => i.Login, new { htmlAttributes = new { @id = "txtLogin", @class = "form-control", @placeholder = "Введите логин" } })
</div>
@Html.ValidationMessageFor(i => i.Login, "", new { @class = "text-danger" })
</div>
</div>

<div class="form-group">
@Html.LabelFor(i => i.Password, "Пароль")
<div class="cols-sm-10">
<div class="input-group">
<span class="input-group-text"><i class="fa fa-lock fa" aria-hidden="true"></i></span>
@Html.PasswordFor(i => i.Password, new { @id = "txtPass", @class = "form-control", @placeholder = "Введите пароль" })
</div>
@Html.ValidationMessageFor(i => i.Password, "", new { @class = "text-danger" })
</div>
</div>

<div class="form-group">
@Html.LabelFor(i => i.Email, "E-Mail")
<div class="cols-sm-10">
<div class="input-group">
<span class="input-group-text"><i class="fa fa-envelope fa" aria-hidden="true"></i></span>
@Html.EditorFor(i => i.Email, new { htmlAttributes = new { @id = "txtMail", @class = "form-control", @placeholder = "Введите e-mail", @type = "email" } })
</div>
@Html.ValidationMessageFor(i => i.Email, "", new { @class = "text-danger" })
</div>
</div>

<input id="btnAdd" type="submit" class="btn btn-success btn-block" value="Добавить" />
</div>
</div>
}

<script type="text/javascript">
$(document).ready(function () {
$('form').validate({
submitHandler: function (form) {
$.ajax({
type: "POST",
url: "/Authorization/Registration",
data: $(form).serialize(),
success: function (result) {
$(location).attr('href', result.URL);
}
});
}
});
});




Controller:



[HttpPost]
public ActionResult Registration(Client client)
{
if (ModelState.IsValid)
{
/*unitOfWork.Clients.Create(client);
unitOfWork.Save();*/
var url = new { URL = Url.Content("/Authorization/Index") + "#success" };
return Json(url);
}

return View();
}


_Layout:



@Scripts.Render("~/bundles/jquery")
@Scripts.Render("~/bundles/bootstrap")
@Scripts.Render("~/scripts/jquery.unobtrusive-ajax.js")
@Scripts.Render("~/scripts/jquery.validate.min.js")
@Scripts.Render("~/scripts/jquery.validate.unobtrusive.min.js")

<div class="container body-content">
@RenderBody()
</div>


With this option validation works, but only after the form is submitted. And after performing the Ajax request, it does not redirect me to the address that I collect in the controller, but instead simply displays this address as a string on the screen. And if instead of using jquery Ajax.Beginform, then, validation does not work when the form has already been submitted, but immediately after entering incorrect data in the input field.
enter image description here










share|improve this question















marked as duplicate by Liam, Tetsuya Yamamoto, Sparky jquery
Users with the  jquery badge can single-handedly close jquery questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Jan 4 at 18:08


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.



















  • You need to use jQuery.validate library as provided in this issue and set validation rules depending on requirements.

    – Tetsuya Yamamoto
    Jan 3 at 8:00











  • Try: stackoverflow.com/a/52208543/594235

    – Sparky
    Jan 5 at 19:35











  • @Sparky, i try it's, but not work. I give an example of my code at the end of the question.

    – Андрей
    Jan 6 at 16:29











  • @Sparky It turned out to make so that validation would work immediately when entering data into text fields. Forgot to add first $(document).ready, but there was another problem. After performing the Ajax request, the program does not transfer to the page with the address I return to the controller, but simply writes this address as a string on the screen. The screenshot at the end of the question.

    – Андрей
    Jan 6 at 17:32











  • @Sparky And what is most interesting, in the debugging of the client, the code does not even enter the block of the succession. Then I decided to check on the server Request.IsAjaxRequest and result 'false'. Why the server does not consider my request, Ajax request. And how to fix it? What's wrong with my code?

    – Андрей
    Jan 6 at 17:33
















0
















This question already has an answer here:




  • jQuery Validate SubmitHandler with Ajax Submission Not being hit

    1 answer



  • Jquery post and unobtrusive ajax validation not working mvc 4

    1 answer




I'm trying to validate fields with ValidationMessageFor, and everything works fine if you submit the form using Html.Beginform, but if I use an AJAX query, then validations no longer work.
My Code
View:



@model AutoStore.Domain.Core.Client

<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css" />

<style>
html,
body {
height: 100%;
}

body {
display: -ms-flexbox;
display: flex;
-ms-flex-align: center;
align-items: center;
padding-top: 40px;
padding-bottom: 40px;
}

.form-signin {
width: 100%;
max-width: 330px;
padding: 15px;
margin: auto;
}

.input-group-text {
border-top-left-radius: 30px;
border-bottom-left-radius: 30px;
}

.form-control {
border-radius: 30px;
}
</style>

@Html.ActionLink("Главная", "Index")

@using (Html.BeginForm())
{
<div class="container">
<div class="form-signin">
<div class="form-group">
@Html.LabelFor(i => i.CSurname, "Фамилия")
<div class="cols-sm-10">
<div class="input-group">
<span class="input-group-text"><i class="fa fa-id-badge" aria-hidden="true"></i></span>
@Html.EditorFor(i => i.CSurname, new { htmlAttributes = new { @id = "txtSurname", @class = "form-control", @placeholder = "Введите фамилию" } })
</div>
@Html.ValidationMessageFor(i => i.CSurname, "", new { @class = "text-danger" })
</div>
</div>

<div class="form-group">
@Html.LabelFor(i => i.CName, "Имя")
<div class="cols-sm-10">
<div class="input-group">
<span class="input-group-text"><i class="fa fa-id-badge" aria-hidden="true"></i></span>
@Html.EditorFor(i => i.CName, new { htmlAttributes = new { @id = "txtName", @class = "form-control", @placeholder = "Введите имя" } })
</div>
@Html.ValidationMessageFor(i => i.CName, "", new { @class = "text-danger" })
</div>
</div>

<div class="form-group">
@Html.LabelFor(i => i.CPatronymic, "Отчество")
<div class="cols-sm-10">
<div class="input-group">
<span class="input-group-text"><i class="fa fa-id-badge" aria-hidden="true"></i></span>
@Html.EditorFor(i => i.CPatronymic, new { htmlAttributes = new { @id = "txtPatr", @class = "form-control", @placeholder = "Введите отчество" } })
</div>
@Html.ValidationMessageFor(i => i.CPatronymic, "", new { @class = "text-danger" })
</div>
</div>

<div class="form-group">
@Html.LabelFor(i => i.Login, "Логин")
<div class="cols-sm-10">
<div class="input-group">
<span class="input-group-text"><i class="fa fa-user fa" aria-hidden="true"></i></span>
@Html.EditorFor(i => i.Login, new { htmlAttributes = new { @id = "txtLogin", @class = "form-control", @placeholder = "Введите логин" } })
</div>
@Html.ValidationMessageFor(i => i.Login, "", new { @class = "text-danger" })
</div>
</div>

<div class="form-group">
@Html.LabelFor(i => i.Password, "Пароль")
<div class="cols-sm-10">
<div class="input-group">
<span class="input-group-text"><i class="fa fa-lock fa" aria-hidden="true"></i></span>
@Html.PasswordFor(i => i.Password, new { @id = "txtPass", @class = "form-control", @placeholder = "Введите пароль" })
</div>
@Html.ValidationMessageFor(i => i.Password, "", new { @class = "text-danger" })
</div>
</div>

<div class="form-group">
@Html.LabelFor(i => i.Email, "E-Mail")
<div class="cols-sm-10">
<div class="input-group">
<span class="input-group-text"><i class="fa fa-envelope fa" aria-hidden="true"></i></span>
@Html.EditorFor(i => i.Email, new { htmlAttributes = new { @id = "txtMail", @class = "form-control", @placeholder = "Введите e-mail", @type = "email" } })
</div>
@Html.ValidationMessageFor(i => i.Email, "", new { @class = "text-danger" })
</div>
</div>

<input id="btnAdd" type="submit" class="btn btn-success btn-block" value="Добавить" />
</div>
</div>
}

<script type="text/javascript">
$(document).ready(function () {
$('form').validate({
submitHandler: function (form) {
$.ajax({
type: "POST",
url: "/Authorization/Registration",
data: $(form).serialize(),
success: function (result) {
$(location).attr('href', result.URL);
}
});
}
});
});




Controller:



[HttpPost]
public ActionResult Registration(Client client)
{
if (ModelState.IsValid)
{
/*unitOfWork.Clients.Create(client);
unitOfWork.Save();*/
var url = new { URL = Url.Content("/Authorization/Index") + "#success" };
return Json(url);
}

return View();
}


_Layout:



@Scripts.Render("~/bundles/jquery")
@Scripts.Render("~/bundles/bootstrap")
@Scripts.Render("~/scripts/jquery.unobtrusive-ajax.js")
@Scripts.Render("~/scripts/jquery.validate.min.js")
@Scripts.Render("~/scripts/jquery.validate.unobtrusive.min.js")

<div class="container body-content">
@RenderBody()
</div>


With this option validation works, but only after the form is submitted. And after performing the Ajax request, it does not redirect me to the address that I collect in the controller, but instead simply displays this address as a string on the screen. And if instead of using jquery Ajax.Beginform, then, validation does not work when the form has already been submitted, but immediately after entering incorrect data in the input field.
enter image description here










share|improve this question















marked as duplicate by Liam, Tetsuya Yamamoto, Sparky jquery
Users with the  jquery badge can single-handedly close jquery questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Jan 4 at 18:08


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.



















  • You need to use jQuery.validate library as provided in this issue and set validation rules depending on requirements.

    – Tetsuya Yamamoto
    Jan 3 at 8:00











  • Try: stackoverflow.com/a/52208543/594235

    – Sparky
    Jan 5 at 19:35











  • @Sparky, i try it's, but not work. I give an example of my code at the end of the question.

    – Андрей
    Jan 6 at 16:29











  • @Sparky It turned out to make so that validation would work immediately when entering data into text fields. Forgot to add first $(document).ready, but there was another problem. After performing the Ajax request, the program does not transfer to the page with the address I return to the controller, but simply writes this address as a string on the screen. The screenshot at the end of the question.

    – Андрей
    Jan 6 at 17:32











  • @Sparky And what is most interesting, in the debugging of the client, the code does not even enter the block of the succession. Then I decided to check on the server Request.IsAjaxRequest and result 'false'. Why the server does not consider my request, Ajax request. And how to fix it? What's wrong with my code?

    – Андрей
    Jan 6 at 17:33














0












0








0









This question already has an answer here:




  • jQuery Validate SubmitHandler with Ajax Submission Not being hit

    1 answer



  • Jquery post and unobtrusive ajax validation not working mvc 4

    1 answer




I'm trying to validate fields with ValidationMessageFor, and everything works fine if you submit the form using Html.Beginform, but if I use an AJAX query, then validations no longer work.
My Code
View:



@model AutoStore.Domain.Core.Client

<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css" />

<style>
html,
body {
height: 100%;
}

body {
display: -ms-flexbox;
display: flex;
-ms-flex-align: center;
align-items: center;
padding-top: 40px;
padding-bottom: 40px;
}

.form-signin {
width: 100%;
max-width: 330px;
padding: 15px;
margin: auto;
}

.input-group-text {
border-top-left-radius: 30px;
border-bottom-left-radius: 30px;
}

.form-control {
border-radius: 30px;
}
</style>

@Html.ActionLink("Главная", "Index")

@using (Html.BeginForm())
{
<div class="container">
<div class="form-signin">
<div class="form-group">
@Html.LabelFor(i => i.CSurname, "Фамилия")
<div class="cols-sm-10">
<div class="input-group">
<span class="input-group-text"><i class="fa fa-id-badge" aria-hidden="true"></i></span>
@Html.EditorFor(i => i.CSurname, new { htmlAttributes = new { @id = "txtSurname", @class = "form-control", @placeholder = "Введите фамилию" } })
</div>
@Html.ValidationMessageFor(i => i.CSurname, "", new { @class = "text-danger" })
</div>
</div>

<div class="form-group">
@Html.LabelFor(i => i.CName, "Имя")
<div class="cols-sm-10">
<div class="input-group">
<span class="input-group-text"><i class="fa fa-id-badge" aria-hidden="true"></i></span>
@Html.EditorFor(i => i.CName, new { htmlAttributes = new { @id = "txtName", @class = "form-control", @placeholder = "Введите имя" } })
</div>
@Html.ValidationMessageFor(i => i.CName, "", new { @class = "text-danger" })
</div>
</div>

<div class="form-group">
@Html.LabelFor(i => i.CPatronymic, "Отчество")
<div class="cols-sm-10">
<div class="input-group">
<span class="input-group-text"><i class="fa fa-id-badge" aria-hidden="true"></i></span>
@Html.EditorFor(i => i.CPatronymic, new { htmlAttributes = new { @id = "txtPatr", @class = "form-control", @placeholder = "Введите отчество" } })
</div>
@Html.ValidationMessageFor(i => i.CPatronymic, "", new { @class = "text-danger" })
</div>
</div>

<div class="form-group">
@Html.LabelFor(i => i.Login, "Логин")
<div class="cols-sm-10">
<div class="input-group">
<span class="input-group-text"><i class="fa fa-user fa" aria-hidden="true"></i></span>
@Html.EditorFor(i => i.Login, new { htmlAttributes = new { @id = "txtLogin", @class = "form-control", @placeholder = "Введите логин" } })
</div>
@Html.ValidationMessageFor(i => i.Login, "", new { @class = "text-danger" })
</div>
</div>

<div class="form-group">
@Html.LabelFor(i => i.Password, "Пароль")
<div class="cols-sm-10">
<div class="input-group">
<span class="input-group-text"><i class="fa fa-lock fa" aria-hidden="true"></i></span>
@Html.PasswordFor(i => i.Password, new { @id = "txtPass", @class = "form-control", @placeholder = "Введите пароль" })
</div>
@Html.ValidationMessageFor(i => i.Password, "", new { @class = "text-danger" })
</div>
</div>

<div class="form-group">
@Html.LabelFor(i => i.Email, "E-Mail")
<div class="cols-sm-10">
<div class="input-group">
<span class="input-group-text"><i class="fa fa-envelope fa" aria-hidden="true"></i></span>
@Html.EditorFor(i => i.Email, new { htmlAttributes = new { @id = "txtMail", @class = "form-control", @placeholder = "Введите e-mail", @type = "email" } })
</div>
@Html.ValidationMessageFor(i => i.Email, "", new { @class = "text-danger" })
</div>
</div>

<input id="btnAdd" type="submit" class="btn btn-success btn-block" value="Добавить" />
</div>
</div>
}

<script type="text/javascript">
$(document).ready(function () {
$('form').validate({
submitHandler: function (form) {
$.ajax({
type: "POST",
url: "/Authorization/Registration",
data: $(form).serialize(),
success: function (result) {
$(location).attr('href', result.URL);
}
});
}
});
});




Controller:



[HttpPost]
public ActionResult Registration(Client client)
{
if (ModelState.IsValid)
{
/*unitOfWork.Clients.Create(client);
unitOfWork.Save();*/
var url = new { URL = Url.Content("/Authorization/Index") + "#success" };
return Json(url);
}

return View();
}


_Layout:



@Scripts.Render("~/bundles/jquery")
@Scripts.Render("~/bundles/bootstrap")
@Scripts.Render("~/scripts/jquery.unobtrusive-ajax.js")
@Scripts.Render("~/scripts/jquery.validate.min.js")
@Scripts.Render("~/scripts/jquery.validate.unobtrusive.min.js")

<div class="container body-content">
@RenderBody()
</div>


With this option validation works, but only after the form is submitted. And after performing the Ajax request, it does not redirect me to the address that I collect in the controller, but instead simply displays this address as a string on the screen. And if instead of using jquery Ajax.Beginform, then, validation does not work when the form has already been submitted, but immediately after entering incorrect data in the input field.
enter image description here










share|improve this question

















This question already has an answer here:




  • jQuery Validate SubmitHandler with Ajax Submission Not being hit

    1 answer



  • Jquery post and unobtrusive ajax validation not working mvc 4

    1 answer




I'm trying to validate fields with ValidationMessageFor, and everything works fine if you submit the form using Html.Beginform, but if I use an AJAX query, then validations no longer work.
My Code
View:



@model AutoStore.Domain.Core.Client

<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css" />

<style>
html,
body {
height: 100%;
}

body {
display: -ms-flexbox;
display: flex;
-ms-flex-align: center;
align-items: center;
padding-top: 40px;
padding-bottom: 40px;
}

.form-signin {
width: 100%;
max-width: 330px;
padding: 15px;
margin: auto;
}

.input-group-text {
border-top-left-radius: 30px;
border-bottom-left-radius: 30px;
}

.form-control {
border-radius: 30px;
}
</style>

@Html.ActionLink("Главная", "Index")

@using (Html.BeginForm())
{
<div class="container">
<div class="form-signin">
<div class="form-group">
@Html.LabelFor(i => i.CSurname, "Фамилия")
<div class="cols-sm-10">
<div class="input-group">
<span class="input-group-text"><i class="fa fa-id-badge" aria-hidden="true"></i></span>
@Html.EditorFor(i => i.CSurname, new { htmlAttributes = new { @id = "txtSurname", @class = "form-control", @placeholder = "Введите фамилию" } })
</div>
@Html.ValidationMessageFor(i => i.CSurname, "", new { @class = "text-danger" })
</div>
</div>

<div class="form-group">
@Html.LabelFor(i => i.CName, "Имя")
<div class="cols-sm-10">
<div class="input-group">
<span class="input-group-text"><i class="fa fa-id-badge" aria-hidden="true"></i></span>
@Html.EditorFor(i => i.CName, new { htmlAttributes = new { @id = "txtName", @class = "form-control", @placeholder = "Введите имя" } })
</div>
@Html.ValidationMessageFor(i => i.CName, "", new { @class = "text-danger" })
</div>
</div>

<div class="form-group">
@Html.LabelFor(i => i.CPatronymic, "Отчество")
<div class="cols-sm-10">
<div class="input-group">
<span class="input-group-text"><i class="fa fa-id-badge" aria-hidden="true"></i></span>
@Html.EditorFor(i => i.CPatronymic, new { htmlAttributes = new { @id = "txtPatr", @class = "form-control", @placeholder = "Введите отчество" } })
</div>
@Html.ValidationMessageFor(i => i.CPatronymic, "", new { @class = "text-danger" })
</div>
</div>

<div class="form-group">
@Html.LabelFor(i => i.Login, "Логин")
<div class="cols-sm-10">
<div class="input-group">
<span class="input-group-text"><i class="fa fa-user fa" aria-hidden="true"></i></span>
@Html.EditorFor(i => i.Login, new { htmlAttributes = new { @id = "txtLogin", @class = "form-control", @placeholder = "Введите логин" } })
</div>
@Html.ValidationMessageFor(i => i.Login, "", new { @class = "text-danger" })
</div>
</div>

<div class="form-group">
@Html.LabelFor(i => i.Password, "Пароль")
<div class="cols-sm-10">
<div class="input-group">
<span class="input-group-text"><i class="fa fa-lock fa" aria-hidden="true"></i></span>
@Html.PasswordFor(i => i.Password, new { @id = "txtPass", @class = "form-control", @placeholder = "Введите пароль" })
</div>
@Html.ValidationMessageFor(i => i.Password, "", new { @class = "text-danger" })
</div>
</div>

<div class="form-group">
@Html.LabelFor(i => i.Email, "E-Mail")
<div class="cols-sm-10">
<div class="input-group">
<span class="input-group-text"><i class="fa fa-envelope fa" aria-hidden="true"></i></span>
@Html.EditorFor(i => i.Email, new { htmlAttributes = new { @id = "txtMail", @class = "form-control", @placeholder = "Введите e-mail", @type = "email" } })
</div>
@Html.ValidationMessageFor(i => i.Email, "", new { @class = "text-danger" })
</div>
</div>

<input id="btnAdd" type="submit" class="btn btn-success btn-block" value="Добавить" />
</div>
</div>
}

<script type="text/javascript">
$(document).ready(function () {
$('form').validate({
submitHandler: function (form) {
$.ajax({
type: "POST",
url: "/Authorization/Registration",
data: $(form).serialize(),
success: function (result) {
$(location).attr('href', result.URL);
}
});
}
});
});




Controller:



[HttpPost]
public ActionResult Registration(Client client)
{
if (ModelState.IsValid)
{
/*unitOfWork.Clients.Create(client);
unitOfWork.Save();*/
var url = new { URL = Url.Content("/Authorization/Index") + "#success" };
return Json(url);
}

return View();
}


_Layout:



@Scripts.Render("~/bundles/jquery")
@Scripts.Render("~/bundles/bootstrap")
@Scripts.Render("~/scripts/jquery.unobtrusive-ajax.js")
@Scripts.Render("~/scripts/jquery.validate.min.js")
@Scripts.Render("~/scripts/jquery.validate.unobtrusive.min.js")

<div class="container body-content">
@RenderBody()
</div>


With this option validation works, but only after the form is submitted. And after performing the Ajax request, it does not redirect me to the address that I collect in the controller, but instead simply displays this address as a string on the screen. And if instead of using jquery Ajax.Beginform, then, validation does not work when the form has already been submitted, but immediately after entering incorrect data in the input field.
enter image description here





This question already has an answer here:




  • jQuery Validate SubmitHandler with Ajax Submission Not being hit

    1 answer



  • Jquery post and unobtrusive ajax validation not working mvc 4

    1 answer








jquery ajax asp.net-mvc jquery-validate unobtrusive-validation






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 6 at 17:35







Андрей

















asked Jan 3 at 7:52









АндрейАндрей

1135




1135




marked as duplicate by Liam, Tetsuya Yamamoto, Sparky jquery
Users with the  jquery badge can single-handedly close jquery questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Jan 4 at 18:08


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.









marked as duplicate by Liam, Tetsuya Yamamoto, Sparky jquery
Users with the  jquery badge can single-handedly close jquery questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Jan 4 at 18:08


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.















  • You need to use jQuery.validate library as provided in this issue and set validation rules depending on requirements.

    – Tetsuya Yamamoto
    Jan 3 at 8:00











  • Try: stackoverflow.com/a/52208543/594235

    – Sparky
    Jan 5 at 19:35











  • @Sparky, i try it's, but not work. I give an example of my code at the end of the question.

    – Андрей
    Jan 6 at 16:29











  • @Sparky It turned out to make so that validation would work immediately when entering data into text fields. Forgot to add first $(document).ready, but there was another problem. After performing the Ajax request, the program does not transfer to the page with the address I return to the controller, but simply writes this address as a string on the screen. The screenshot at the end of the question.

    – Андрей
    Jan 6 at 17:32











  • @Sparky And what is most interesting, in the debugging of the client, the code does not even enter the block of the succession. Then I decided to check on the server Request.IsAjaxRequest and result 'false'. Why the server does not consider my request, Ajax request. And how to fix it? What's wrong with my code?

    – Андрей
    Jan 6 at 17:33



















  • You need to use jQuery.validate library as provided in this issue and set validation rules depending on requirements.

    – Tetsuya Yamamoto
    Jan 3 at 8:00











  • Try: stackoverflow.com/a/52208543/594235

    – Sparky
    Jan 5 at 19:35











  • @Sparky, i try it's, but not work. I give an example of my code at the end of the question.

    – Андрей
    Jan 6 at 16:29











  • @Sparky It turned out to make so that validation would work immediately when entering data into text fields. Forgot to add first $(document).ready, but there was another problem. After performing the Ajax request, the program does not transfer to the page with the address I return to the controller, but simply writes this address as a string on the screen. The screenshot at the end of the question.

    – Андрей
    Jan 6 at 17:32











  • @Sparky And what is most interesting, in the debugging of the client, the code does not even enter the block of the succession. Then I decided to check on the server Request.IsAjaxRequest and result 'false'. Why the server does not consider my request, Ajax request. And how to fix it? What's wrong with my code?

    – Андрей
    Jan 6 at 17:33

















You need to use jQuery.validate library as provided in this issue and set validation rules depending on requirements.

– Tetsuya Yamamoto
Jan 3 at 8:00





You need to use jQuery.validate library as provided in this issue and set validation rules depending on requirements.

– Tetsuya Yamamoto
Jan 3 at 8:00













Try: stackoverflow.com/a/52208543/594235

– Sparky
Jan 5 at 19:35





Try: stackoverflow.com/a/52208543/594235

– Sparky
Jan 5 at 19:35













@Sparky, i try it's, but not work. I give an example of my code at the end of the question.

– Андрей
Jan 6 at 16:29





@Sparky, i try it's, but not work. I give an example of my code at the end of the question.

– Андрей
Jan 6 at 16:29













@Sparky It turned out to make so that validation would work immediately when entering data into text fields. Forgot to add first $(document).ready, but there was another problem. After performing the Ajax request, the program does not transfer to the page with the address I return to the controller, but simply writes this address as a string on the screen. The screenshot at the end of the question.

– Андрей
Jan 6 at 17:32





@Sparky It turned out to make so that validation would work immediately when entering data into text fields. Forgot to add first $(document).ready, but there was another problem. After performing the Ajax request, the program does not transfer to the page with the address I return to the controller, but simply writes this address as a string on the screen. The screenshot at the end of the question.

– Андрей
Jan 6 at 17:32













@Sparky And what is most interesting, in the debugging of the client, the code does not even enter the block of the succession. Then I decided to check on the server Request.IsAjaxRequest and result 'false'. Why the server does not consider my request, Ajax request. And how to fix it? What's wrong with my code?

– Андрей
Jan 6 at 17:33





@Sparky And what is most interesting, in the debugging of the client, the code does not even enter the block of the succession. Then I decided to check on the server Request.IsAjaxRequest and result 'false'. Why the server does not consider my request, Ajax request. And how to fix it? What's wrong with my code?

– Андрей
Jan 6 at 17:33












0






active

oldest

votes

















0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes

Popular posts from this blog

Monofisismo

Angular Downloading a file using contenturl with Basic Authentication

Olmecas