Currently, I am in the process of migrating an ASP.net Web Forms application to MVC.
This application makes use of AJAX through an Ajax-enabled WCF Web service
and asp:ScriptManager
. I have been sending an array of objects to the service, and it has been handling it without any issues. Here is a snippet of the code:
<script type="text/javascript">
$().ready(function () {
var ser = new Services.TasksService();
$('#tasks').tasksgrid(
'newTaskName',
'createTask',
'submitData',
loadData,
submitData,
deleteData
);
function loadData(callback) {
return ser.GetAllTasks(callback, null, null);
}
function submitData(data, callback) {
return ser.Submit(data, callback, null, null);
}
function deleteData(data, callback) {
return ser.Delete(data, callback, null, null);
}
}
);
</script>
On the WCF service side, the code looks like this:
[ServiceContract(Namespace = "Services")]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class TasksService
{
[OperationContract]
public IList<Task> GetAllTasks()
{
//Code..
}
[OperationContract]
public void Submit(IList<Task> tasks)
{
//Code..
}
[OperationContract]
public void Delete(IList<Task> tasks)
{
//Code..
}
}
The Submit and Delete methods receive an Array of Task objects. Creating these arrays dynamically on the client-side and passing them to the appropriate Services.TasksService
works seamlessly with the WCF infrastructure.
Now, I am moving away from the WCF service and attempting to achieve the same functionality using a Controller class. While GetAllTasks method worked fine, I encountered difficulties with the data receiving methods.
In the controller, I have:
[HttpPost]
public JsonResult Submit(IList<Task> tasks)
{
And on the client side:
function submitData(data, callback) {
$.post('/Tasks/Submit', JSON.stringify(data), callback, 'json');
}
Despite trying different approaches, I consistently receive null as the tasks object (indicating that the data is not being bound).
I came across a post by Phil Haack addressing this issue, but I would prefer to avoid using additional assemblies if possible.