weixin_33676492 2016-08-23 08:42 采纳率: 0%
浏览 95

ASP.NET Core AJAX无法正常工作吗?

I am trying to implement AJAX in ASP.NET Core so I have created a new default "ASP.NET Core Web Application (.NET Core) with "Individual User Accounts".

I added "Microsoft.jQuery.Unobtrusive.Ajax": "3.2.3" at project.json under "dependencies" section.

for "Register.cshtml" I have changed the declaration to

<form asp-controller="Account" asp-action="Register" asp-route-returnurl="@ViewData["ReturnUrl"]" data-ajax="true" data-ajax-method="POST" class="form-horizontal">

at "Register" action in AccountController I have changed it to check if the post is using AJAX.

[HttpPost]
    [AllowAnonymous]
    [ValidateAntiForgeryToken]
    public async Task<IActionResult> Register(RegisterViewModel model, string returnUrl = null)
    {
        var isAjax = HttpContext.Request.Headers["X-Requested-With"] == "XMLHttpRequest";

        ViewData["ReturnUrl"] = returnUrl;
        if (ModelState.IsValid)
        {
            var user = new ApplicationUser { UserName = model.Email, Email = model.Email };
            var result = await _userManager.CreateAsync(user, model.Password);
            if (result.Succeeded)
            {
                // For more information on how to enable account confirmation and password reset please visit http://go.microsoft.com/fwlink/?LinkID=532713
                // Send an email with this link
                //var code = await _userManager.GenerateEmailConfirmationTokenAsync(user);
                //var callbackUrl = Url.Action("ConfirmEmail", "Account", new { userId = user.Id, code = code }, protocol: HttpContext.Request.Scheme);
                //await _emailSender.SendEmailAsync(model.Email, "Confirm your account",
                //    $"Please confirm your account by clicking this link: <a href='{callbackUrl}'>link</a>");
                await _signInManager.SignInAsync(user, isPersistent: false);
                _logger.LogInformation(3, "User created a new account with password.");
                return RedirectToLocal(returnUrl);
            }
            AddErrors(result);
        }

        // If we got this far, something failed, redisplay form
        return View(model);
    }

The problem is that isAjax always returns false.

Thanks in Advanced for any help, AG

  • 写回答

1条回答 默认 最新

  • weixin_33726318 2016-08-24 08:02
    关注

    I have added "jquery-ajax-unobtrusive": "3.2.4" thru bower.json and then a reference to jquery.unobtrusive-ajax.js and now it is working. Thanks @jerome for your help

    评论

报告相同问题?

悬赏问题

  • ¥15 yolov8边框坐标
  • ¥15 matlab中使用gurobi时报错
  • ¥15 WPF 大屏看板表格背景图片设置
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真