1

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?

1 Answer 1

1

Try this:

data: 
{
     Text: $('#EditPostViewModel_Text').val(),
     Title: $('#EditPostViewModel_Title').val() 
}
Sign up to request clarification or add additional context in comments.

1 Comment

To the OP: you need to provide a valid JS object to the AJAX method; your syntax (using = instead of :) won't produce a valid object.

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.