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

Send kendo.data.Model object back to ASP.MVC

1 Answer 66 Views
Grid
This is a migrated thread and some comments may be shown as answers.
JeeShen
Top achievements
Rank 2
JeeShen asked on 27 Nov 2012, 10:30 AM
How do I send the object constructed using kendo.data.Model back to ASP.MVC?

For example, a person object is create using the kendo.data.Model using the code below
var Person = kendo.data.Model.define( {
    id: "personId", // the identifier of the model
    fields: {
        "name": {
            type: "string"
        },
        "age": {
            type: "number"
        }
    }
});
 
var person = new Person( {
    name: "John Doe",
    age: 42
});
My first question - How do I use a $.Ajax() to send the object back to my ASP MVC action (e.g AddPerson)?
function addPerson(person) {
    $.ajax({                   
        url: "/Person/Add/"
    });
}

My second question - How do i bind the receive model on the server side code?
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Add([DataSourceRequest] DataSourceRequest request, Person person)
{
    // Code to add person to database.
}

1 Answer, 1 is accepted

Sort by
0
Accepted
Petur Subev
Telerik team
answered on 30 Nov 2012, 08:51 AM
Hello JeeShen,

You need to call toJSON() method of your model before sending it as data through the $.ajax method.

e.g.
<script type="text/javascript">
    $(function () {
        var Person = kendo.data.Model.define({
            id: "personId", // the identifier of the model
            fields: {
                "name": {
                    type: "string"
                },
                "age": {
                    type: "number"
                }
            }
        });
 
        var person = new Person({
            name: "John Doe",
            age: 42
        });
 
        $.ajax({
            url: "/Home/Index/",
            data: person.toJSON()
        });
    })   
</script>

public ActionResult Index(Person person)
{


All the best,
Petur Subev
the Telerik team
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
Tags
Grid
Asked by
JeeShen
Top achievements
Rank 2
Answers by
Petur Subev
Telerik team
Share this question
or