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

JQuery Ajax POST字符串参数在MVC Action方法中获取为null的问题排查

Troubleshooting Null Parameters in MVC Action with jQuery

Hey, I’ve been in your exact spot before—spending hours trying to figure out why an MVC action keeps getting null parameters even after trying all the usual fixes like JSON.stringify. Super frustrating when you think it should be straightforward! Let’s go through the most common reasons this happens, especially since adjusting the URL makes it work:

  • Mismatched Parameter Names
    Double-check that the parameter name in your jQuery call matches exactly what’s in your MVC Action method. MVC uses name-based binding, so even a tiny difference (like userId vs userid, or camelCase vs PascalCase) will break it. For example, if your action has public ActionResult SubmitData(string orderId), your AJAX data needs to include orderId: yourValue, not a misspelled variant.

  • Incorrect Content-Type Header
    If you’re using JSON.stringify to send data, you must set the contentType to "application/json" in your jQuery AJAX setup. Without this, MVC won’t parse the JSON payload correctly, leading to nulls. Here’s a quick example of the right setup:

    $.ajax({
        url: '/Orders/SubmitData',
        type: 'POST',
        contentType: 'application/json',
        data: JSON.stringify({ orderId: '12345' }),
        success: function(response) {
            // Handle response
        }
    });
    

    If you’re sending form data instead of JSON, leave out the contentType (jQuery defaults to the correct form-encoded type automatically).

  • Route Configuration Conflicts
    Since modifying the URL fixes the issue, your route setup is probably conflicting with your parameter. Check your route definitions (either in RouteConfig.cs or via attribute routes on your action) to make sure the URL pattern matches what you’re sending. For example, if your route expects a required {id} segment but you’re appending a query string parameter that overlaps, MVC might be trying to bind the wrong value.

  • Query String Encoding Issues
    If you’re passing parameters via the URL query string (instead of the POST body), special characters (spaces, &, ?, etc.) might not be encoded properly. Use encodeURIComponent() on your parameter values to ensure they’re URL-safe:

    var orderId = 'ORD-123 & 456';
    var url = '/Orders/SubmitData?orderId=' + encodeURIComponent(orderId);
    
  • Complex Model Binding Gotchas
    If you’re sending a complex object instead of a simple string/number, make sure your model has a parameterless constructor (MVC needs this to create an instance) and that all nested property names match exactly what’s in your JSON.

If you can share snippets of your actual jQuery code and MVC Action method, we can pinpoint the exact issue even faster!

内容的提问来源于stack exchange,提问作者Xiao Han

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.19 10:31:50