This is a migrated thread and some comments may be shown as answers.

Batch operation with asp.net mvc4 webapi

0 Answers 97 Views
Data Source
This is a migrated thread and some comments may be shown as answers.
Nick Wu
Top achievements
Rank 1
Nick Wu asked on 01 Apr 2012, 04:51 AM
use this api controller:
public class PersonController:ApiController
{
      // other codes...
       public HttpResponseMessage PostNewPersons(IEnumerable<PesonDTO> people)
        {
            foreach (PersonDTO dto in people)
                _personAppSrv.Create(dto);
            return new HttpResponseMessage(HttpStatusCode.Created);
        }
    // other codes...
}

use DataSource in webpage:

var ds = new kendo.data.DataSource(
{
transport:{
create: {
                        url:  "/api/person",
                        dataType: "json",
                        type:"Post",
                        contentType: 'application/json;charset=utf-8'    // important!!
                    },
//other codes
  parameterMap: function (options, operation) {
                        if (operation !== "read" && options.models) {
                            return JSON.stringify(options.models);
                        }
                }
,
batch:true,
//other codes
});

I use gird to run  batch operation like example. If I don't set contentType to 'application/json;' in code above, the request content's contentType is 'application/x-www-form-urlencoded'! Then the api controller can't convert request content data to 'PersonDTO' instance. If I set it, it works well.

If anyone has more better solution, please tell me ,thanks.

No answers yet. Maybe you can help?

Tags
Data Source
Asked by
Nick Wu
Top achievements
Rank 1
Share this question
or