Ivy
Ivy

Reputation: 2305

Make jquery ajax calls to ASP.NET MVC action

I have a simple Edit action in ASP.NET MVC that looks like this :

[HttpPost]
public ActionResult Edit(EditPostViewModel data)
{
}

I am trying to make a post to this action like this :

function SendPost(actionPath) {
    $.ajax({
        url: actionPath,
        type: 'POST',
        dataType: 'json',
        data: '{Text=' + $('#EditPostViewModel_Text').val() + 'Title=' + $('#EditPostViewModel_Title').val() + '}',
        success: function (data) {
            alert('success');
        },
        error: function () {
            alert('error');
        }
    });
}

The action will be triggered but the EditPostViewModel will not be filled with the Text and Title?

Im hoping that I could use a regular ASP.NET MVC action to be able to handle validations on serverside with ModelState.

There will later be code in success and error that handels the returning data.

How is this supose to work?

Upvotes: 1

Views: 1755

Answers (1)

MuriloKunze
MuriloKunze

Reputation: 15583

Try this:

data: 
{
     Text: $('#EditPostViewModel_Text').val(),
     Title: $('#EditPostViewModel_Title').val() 
}

Upvotes: 1

Related Questions