You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Asp.net Core 2.0中Ajax请求无法触发指定PageModel Post处理方法

Hey there! Let's figure out why your AJAX request keeps hitting the default OnPostAsync instead of your targeted OnPostFormalDealerNameAsync method. I'll break down the issues and fixes step by step.

The Core Issue: Razor Pages Handler Routing

Razor Pages uses a specific convention to map requests to handler methods. For named post handlers (like your OnPostFormalDealerNameAsync), you need to explicitly tell the framework which handler to use—either via a query parameter or route value. Your current setup isn't signaling this, so it falls back to the default OnPostAsync.


Fix 1: Update the AJAX Request to Specify the Handler

You need to add the handler parameter to your AJAX URL (or include it in the request data) to point to your named handler. Here's the adjusted JavaScript:

function getFormalDealerName(e) {
    var token = '@GetAntiXsrfRequestToken()';
    $.ajax({
        type: "POST",
        // Add ?handler=FormalDealerName to target your named handler
        url: "/ShoesMarket/Shoes2Dealer?handler=FormalDealerName",
        headers: { "RequestVerificationToken": token },
        data: dealer_str,
        success: function (response) {
            if (response != null) {
                // Handle your successful response here
            } else {
                alert("Something went wrong");
            }
        },
        failure: function (response) {
            alert(response.responseText);
        },
    });
}

Fix 2: Adjust the C# Handler Attributes

The [Route] attribute you added is unnecessary (and conflicting) for Razor Pages, which uses file-based routing by default. Also, the [HttpPost] attribute is redundant since the method name OnPostFormalDealerNameAsync already signals it's a POST handler. Clean up your PageModel code like this:

namespace ShoesSystem.Pages.ShoesMarket.Shoes2Dealer
{
    [Authorize]
    public class IndexModel : PageModel
    {
        private readonly ShoesSystem.Data.ApplicationDbContext _context;

        public IndexModel(ShoesSystem.Data.ApplicationDbContext context)
        {
            _context = context;
        }

        public async Task OnGetAsync()
        { 
            // Your existing GET logic
        }

        public async Task<IActionResult> OnPostAsync()
        { 
            // Your existing default POST logic
        }

        [ValidateAntiForgeryToken]
        // Removed [Route] and redundant [HttpPost]
        public async Task<IActionResult> OnPostFormalDealerNameAsync()
        { 
            // Your targeted handler logic here
        }
    }
}

Why This Works

  • The ?handler=FormalDealerName query parameter tells Razor Pages to look for a handler method named OnPostFormalDealerNameAsync (the framework maps the handler value to the suffix after OnPost).
  • Removing the [Route] attribute avoids overriding the default file-based routing for your page (your IndexModel maps to the /ShoesMarket/Shoes2Dealer path, matching your page file location).

Extra Notes

  • If you prefer to use a route segment instead of a query parameter (e.g., /ShoesMarket/Shoes2Dealer/Dealers), you can update the @page directive in your Index.cshtml file to:
    @page "{handler?}"
    
    Then your AJAX URL can be /ShoesMarket/Shoes2Dealer/Dealers (since Dealers will map to the FormalDealerName handler? Wait no—wait, the handler name needs to match. If you want the route segment to be Dealers, you'd need to name your method OnPostDealersAsync, or use a route constraint. The query parameter approach is simpler for most cases.
  • Ensure your dealer_str is formatted correctly. If it's a JSON object, add contentType: "application/json" to your AJAX call and stringify the data:
    data: JSON.stringify(dealer_str),
    contentType: "application/json",
    

内容的提问来源于stack exchange,提问作者Chunde Huang

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 07:12:45