79721634

Date: 2025-07-31 17:30:00
Score: 0.5
Natty:
Report link

I think your issue lies in a mismatch between the client-side and server-side. In your AJAX call, you are sending a JSON object that looks like this: { "saveStudentdata": { ... your student data ... } }.

You have wrapped your actual student data inside a property named saveStudentdata. Your controller action public JsonResult Create(StudentModel SaveStudentdata) is expecting a StudentModel object directly.

The model binder looks at the incoming data and tries to map its properties (id, Stu_Name, etc.) to the SaveStudentdata parameter. To me, it seems there is extra wrapper on the client-side, the model binder doesn't find a direct match and SaveStudentdata ends up being null or empty etc.

I reference solutions at stackoverflow here source via Stackoverflow

Please try this and see if it helps.




Controller:
[HttpPost] 
public JsonResult Create([FromBody] StudentModel saveStudentdata)
{
 string status = string.Empty;
 try {
 // Your model binding should now work, and saveStudentdata will be populated.
if (!ModelState.IsValid){
  return Json(new { saveStatus = "Error", errorMessage = "Invalid data" });
}
status = Studata.Createdata(saveStudentdata);
return Json(new { saveStatus = status });
    }
catch (Exception ex)
    {
 return Json(new { saveStatus = "Error", errorMessage = ex.Message });
    }
}






Ajax

// Assuming 'GetData' is a JavaScript object with properties
// that match your StudentModel (e.g., Stu_Name, Stu_Address)

$.ajax({
 type: "POST",
url: "/Student/Create",
// Send the object directly. JSON.stringify will convert it to a JSON string.
data: JSON.stringify(GetData),
// Ensure the content type is set to application/json
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (response) {
if (response.saveStatus === "Success") {
alert("Data Successfully Saved...");
} else {
alert("Data not Saved.. Error: " + response.errorMessage);
        }
    },
 error: function (xhr, status, error) {
// More descriptive error handling
alert("An error occurred: " + xhr.responseText);
    }
});
Reasons:
  • Blacklisted phrase (1): stackoverflow
  • Blacklisted phrase (1): Stackoverflow
  • Whitelisted phrase (-1): try this
  • Long answer (-1):
  • Has code block (-0.5):
  • Low reputation (1):
Posted by: Devboy