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

MVC视图向控制器传值问题:POST方法中perId未获取到数据

Hey there, let's troubleshoot why your perId parameter isn't coming through in the POST request. I've run into this exact issue a few times, so here are the most common fixes to check:

1. Double-check parameter name matching (case-sensitive!)

MVC's model binding relies on exact, case-sensitive name matches between your view and controller. So:

  • In your view, confirm the input's name attribute is exactly perId (no typos like perid, PerId, or PERID). Example:
    <!-- Hidden input example -->
    <input type="hidden" name="perId" value="@Model.PerId" />
    <!-- Visible input example -->
    <input type="text" name="perId" value="@Model.PerId" />
    
  • In your controller's POST action, make sure the parameter name matches perfectly:
    [HttpPost]
    public ActionResult YourTargetAction(int perId) // Exact name here
    {
        // Your logic here
    }
    
2. Ensure your form uses POST method

It sounds simple, but it's easy to overlook. Confirm your form tag explicitly sets method="post":

@using (Html.BeginForm("YourTargetAction", "YourController", FormMethod.Post))
{
    <!-- Your form fields go here -->
}

If the method is missing or set to GET, the parameter won't be sent in the request body.

3. Verify view model property alignment (if using a model)

If you're passing a view model instead of a standalone parameter:

  • Make sure your model has a PerId property (matching case if you're using automatic binding):
    public class YourViewModel
    {
        public int PerId { get; set; } // Property name must align with input name
    }
    
  • In the view, use helpers or manual inputs that reference the correct property:
    @model YourViewModel
    @using (Html.BeginForm(...))
    {
        @Html.HiddenFor(m => m.PerId) <!-- Generates name="PerId" automatically -->
        <!-- Or manual input -->
        <input name="PerId" value="@Model.PerId" />
    }
    
  • In the controller, access the value via the model:
    [HttpPost]
    public ActionResult YourTargetAction(YourViewModel model)
    {
        var perId = model.PerId; // Pull value from the model
    }
    
4. Inspect the actual request payload

Use your browser's dev tools (F12) to see what's really being sent:

  1. Open the Network tab
  2. Submit the form
  3. Click the POST request in the list
  4. Check the Form Data (Chrome) or Payload (Firefox) section
    • If perId isn't listed here: The issue is in your view (the input isn't rendering or isn't inside the form)
    • If perId is listed but the controller doesn't get it: Check for type mismatches (e.g., expecting int but sending a non-numeric string)
5. Check for [Bind] attribute restrictions

If you're using [Bind(Include = "...")] or [Bind(Exclude = "...")] on your controller parameter or model, make sure perId is included:

[HttpPost]
public ActionResult YourTargetAction([Bind(Include = "perId, OtherRequiredProperty")] YourViewModel model)
{
    // Ensure perId is in the Include list
}
6. Rule out conflicting route parameters

If your route definition includes a perId parameter, ensure it doesn't override form data. For example:

routes.MapRoute(
    name: "Default",
    url: "{controller}/{action}/{perId}",
    defaults: new { controller = "Home", action = "Index", perId = UrlParameter.Optional }
);

If you're not passing perId in the URL, this should still work, but confirm the route parameter type matches your controller's parameter type (e.g., int vs string).


内容的提问来源于stack exchange,提问作者Doğukan Gözen

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.19 08:23:40