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:
MVC's model binding relies on exact, case-sensitive name matches between your view and controller. So:
- In your view, confirm the input's
nameattribute is exactlyperId(no typos likeperid,PerId, orPERID). 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 }
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.
If you're passing a view model instead of a standalone parameter:
- Make sure your model has a
PerIdproperty (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 }
Use your browser's dev tools (F12) to see what's really being sent:
- Open the Network tab
- Submit the form
- Click the POST request in the list
- Check the Form Data (Chrome) or Payload (Firefox) section
- If
perIdisn't listed here: The issue is in your view (the input isn't rendering or isn't inside the form) - If
perIdis listed but the controller doesn't get it: Check for type mismatches (e.g., expectingintbut sending a non-numeric string)
- If
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 }
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

