AngularJS中如何传递StateId与CityId至ASP.NET MVC控制器?
Hey there! Let's figure out why your StateId and CityId aren't saving while other student fields work perfectly. I've run into similar snags with AngularJS + ASP.NET MVC before, so here are the most likely culprits and actionable fixes to check:
First, confirm your frontend ng-model directives match exactly with your ASP.NET MVC model's property names. C# is case-sensitive, so if your backend model has public int StateId { get; set; }, your frontend should use ng-model="student.StateId" (not a lowercase stateId).
If you're using dropdowns for State/City, a common mistake is binding to the entire object instead of just the ID. For example, this will bind the full State object (not the ID):
<select ng-model="student.State" ng-options="s.Name for s in states">...</select>
Instead, bind directly to the ID and use track by to ensure proper selection:
<select ng-model="student.StateId" ng-options="s.Id as s.Name for s in states track by s.Id">...</select>
Open your browser's DevTools (F12), go to the Network tab, and submit the form. Look at the POST request's Payload or Form Data section:
- If
StateId/CityIdare missing: Your AngularJS scope isn't updating these values. Check for typos inng-modelnames or broken dropdown-to-scope links. - If they show
null/0instead of the selected value: Your dropdown is binding the wrong data—double-check yourng-optionssetup.
On the backend, set a breakpoint in your save action to check if StateId/CityId are being received correctly:
[HttpPost] public ActionResult SaveStudent(StudentViewModel student) { // Breakpoint here to inspect student.StateId and student.CityId if (ModelState.IsValid) { // Save logic return RedirectToAction("Index"); } return View(student); }
If the values are still empty, verify:
- Your
StudentViewModelhasStateId/CityIdproperties with the correct data type (usuallyintorint?for nullable values). - There are no typos in property names (case matters!).
- If using API controllers with
[FromBody], ensure your AngularJS$http.postsends JSON data with the correct content type.
Sometimes StateId/CityId fail validation (e.g., marked as [Required] but no value is sent). In your controller, debug validation errors like this:
foreach (var error in ModelState.Values.SelectMany(v => v.Errors)) { Debug.WriteLine(error.ErrorMessage); }
On the frontend, you can display these errors to users with AngularJS:
<div ng-show="studentForm.StateId.$error.required">Please select a state</div>
If you're updating StateId/CityId programmatically (e.g., via jQuery events), wrap the change in $scope.$apply() to trigger Angular's digest cycle:
$('#stateDropdown').on('change', function() { $scope.$apply(function() { $scope.student.StateId = $(this).val(); }); });
Work through these steps one by one—chances are one of these fixes will resolve the issue. If you can share snippets of your actual script.js, controller, and Index.cshtml code, I can give even more targeted help!
内容的提问来源于stack exchange,提问作者SRP




