0

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
  • Any other ideas, guys? It seems like only one solution is to use MVC Futures, as suggested in Phil Haack post. Commented Sep 22, 2010 at 7:09

2 Answers 2

1

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');
}
Sign up to request clarification or add additional context in comments.

6 Comments

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 ?
Also, could you please link a material, with explanation of "how/why it works"? thanks !
Can you list the struture of the task C# object and the data javascript object?
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: theycallmemrjames.blogspot.com/2010/05/…
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.
|
0

Look here http://theycallmemrjames.blogspot.com/2010/05/aspnet-mvc-and-jquery-part-4-advanced.html

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.