Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

221
Visualizações
.NET MVC 5 Ajax call unable to display javascript message after post

I have made a form in my .NET MVC 5 application like following:

   @using (Ajax.BeginForm("ChangePassword", "User", new { area = "UserSettings" }, new
  AjaxOptions
  {
  HttpMethod = "POST",
  OnSuccess = "alert('success');" //This will execute once the Ajax call is finished.

  }, null))
  {
  @Html.TextBoxFor(m => m.Password, new { placeholder = "Password", @class = "form-control", @type = "password" })<br />
  @Html.ValidationMessageFor(model => model.Password, "", new { @class = "text-danger", @style = "float:right;" })
  @Html.TextBoxFor(m => m.PasswordConfirm, new { placeholder = "Confirm password", @class = "form-control", @type = "password" })<br />
  @Html.ValidationMessageFor(model => model.PasswordConfirm, "", new { @class = "text-danger", @style = "float:right;" })
  <button type="submit" class="btn btn-primary">Change Password</button>
  }

And this is the method in controller:

  [HttpPost]
        [ActionName("ChangePassword")]
        public ActionResult ChangePassword(UserSettings model)
        {
            using (var ctx = new myContextEntities())
            {
                var usr = ctx.Users.FirstOrDefault(x => x.Email == User.Identity.Name);
                model.FirstName = usr.FirstName;
                model.LastName = usr.LastName;
                model.Email = usr.Email;
                model.Country = usr.Countries.CountryName;
                if (ModelState.IsValid)
                {
                    usr.PasswordSalt = Helpers.PasswordHelper.CreateSalt(40);
                    usr.PasswordHash = Helpers.PasswordHelper.CreatePasswordHash(model.Password, usr.PasswordSalt);
                    ctx.SaveChanges();
                    return View("UserSettings",model);
                }
                return View("UserSettings", model);
            }
        }

Once the call is finished the page just refreshes, which shouldn't happen, but it should rather just display the success message ... ? What am I doing wrong here?

about 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

For Ajax request, please create the following bundle in BundleConfig.cs

public static void RegisterBundles(BundleCollection bundles)
{
   //Create Your other bundles here

   //Create your Jquery validate bundle
   bundles.Add(new ScriptBundle("~/bundles/jqueryval").Include(
                "~/Scripts/Assets/jquery.validate.min.js",
                "~/Scripts/Assets/jquery.validate.unobtrusive.min.js",
                "~/Scripts/Assets/jquery.unobtrusive-ajax.min.js"
            ));
}

In your _Layout.cshtml. Include this bundle as:

  @Scripts.Render("~/bundles/jqueryval")

Please make sure, jQuery must be included before this bundle. Please note that the paths to the javascript files are dummy in the above code block. You should give your own paths. The following JS files must be included:

  1. jquery.validate.min.js
  2. jquery.validate.unobtrusive.min.js
  3. jquery.unobtrusive-ajax.min.js

Hope it helps :)

about 4 years ago · Santiago Trujillo Relatório

0

Include Unobtrusive Ajax from the below Link

Click Here

Note: Dont include both jquery.unobtrusive-ajax.min.js and jquery.unobtrusive-ajax.min.js, Include only one (js/min.js) file

The Below Files Does not Affect anything for you and your problem..

1.jquery.validate.min.js
2.jquery.validate.unobtrusive.min.js

about 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda