views:

40

answers:

1

I'm porting an ASP.net Web Forms application to MVC.

The application uses AJAX, by means of Ajax-enabled WCF Web service and asp:ScriptManager. I send an array of objects for service, it handles it just great. Code example,

    <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>

WCF service side code:

    [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/Delete method, recieves Array of Tasks objects. I create those array dynamically in client side script and just put it to corresponding Services.TasksService (no $.toJSON or JSON.stringly call, nothing like that). WCF infrastucture handles it greacefully and I always get a correct object on server.

Now, I'm getting rid of WCF service and try to do the same with Controller class. GetAllTasks were fine.. but I totally missed with 'recieving' data methods.

In controller I have,

        [HttpPost]
    public JsonResult Submit(IList<Task> tasks)
    {

On client,

            function submitData(data, callback) {
            $.post('/Tasks/Submit', JSON.stringify(data), callback, 'json');
        }

But anything I tried, I always recieve null as tasks object (so, data is not binded).

I've seen Phil Haack post on that, but would like to avoid using of any additional assemblies, if possible.

+1  A: 

MVC needs to be told what variable on the server side to bind the data to. In your example you could do the following:

    function submitData(data, callback) {
    $.post('/Tasks/Submit', { tasks: data }, callback, 'json');
}
Clicktricity
It improves the situation, but no completely.. Now, the 'tasks' argument of Submit method is not null, but all properties of the object is not intitilized. Please note, that JS object fields names equals to C# object ones.. What could be wrong ?
alexanderb
Also, could you please link a material, with explanation of "how/why it works"? thanks !
alexanderb
Can you list the struture of the task C# object and the data javascript object?
Clicktricity
For how/why it works, look up default model binding. The general concept is that the "convention over configuration" approach means that MVC assumes that matching object names and property names should be mapped during model binding. For more detailed examples along the lines of what you are doing have a look at: http://theycallmemrjames.blogspot.com/2010/05/aspnet-mvc-and-jquery-part-4-advanced.html
Clicktricity
Task is a class Linq to SQL generated class.. Seems, nothing special, bunch of properties. As I said before, names of properties corresponds to names of JS object fields.
alexanderb
Thanks a lot for link!..
alexanderb
Post data seems to be ok, tasks[0][ActualWork] 106tasks[0][Description] aaatasks[0][Id] 28tasks[0][Number] 3tasks[0][UserId] 0
alexanderb