I am working with a .NET class in C# called BusinessModel:
public class BusinessModel
{
public string BlobName { get; set; }
public string NewName { get; set; }
}
In my MVC Ajax Controller, I have an action called DoBusiness:
[HttpPost]
public ActionResult DoBusiness(string handle, BusinessModel businessData)
{
// Implement business logic here.
}
Initially, I tried invoking this action using this JavaScript code:
var bd = JSON.stringify({ BlobName: "blob", NewName: "new" });
$.ajax({
type: "POST",
url: "/Ajax/DoBusiness",
data: { handle: "MyHandle", businessData: bd },
success: businessComplete,
error: businessFailed
});
Upon inspecting the data sent via Fiddler, it was encoded as follows:
handle=MyHandle&businessData={"BlobName":"blob","NewName":"new"}
However, when checking the values passed to the controller action using Visual Studio debugger, the parameter "handle" had the expected value but "businessData" was null.
I then modified the JavaScript code to remove the JSON.stringify call:
var bd = { BlobName: "blob", NewName: "new" };
$.ajax({
type: "POST",
url: "/Ajax/DoBusiness",
data: { handle: "MyHandle", businessData: bd },
success: businessComplete,
error: businessFailed
});
This time, the data sent via Fiddler was rendered differently:
handle=MyHandle&businessData%5BBlobName%5D=blob&businessData%5BNewName%5D=new
But even in this case, while "handle" was correct, both properties "BlobName" and "NewName" inside "businessData" were set to null in the controller action.
So the question remains: What mistake am I making? How can I properly populate the class in the controller action?
Additional note: This specific situation involves nested Ajax calls where complex objects are passed between them. While this example has been simplified, the core issue remains regarding object population within the controller action context.