2

I have the following JSON Request body (copied from ie10 Admin Panel Network capture)

{"FirstName":"James","LastName":"Jones","Email":"[email protected]"}

My controller is as follows (the x variable is to break on):

[HttpPost]
public void EditPerson(PersonUpdateViewModel person)
{
    int x = 0;
}

My ViewModel is as follows:

public class PersonUpdateViewModel
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string Email { get; set; }
}

The EditPerson Action is reached, I break on the x variable, but all of the properties in the person variable are null, does anyone have any hints for what I might be doing wrong? At this point I would even be ok accepting the raw JSON string and parsing from there but I can't get any parameter into the action method.

javascript function I am posting from by request:

var submitEdit2 = function () {
    var editables = $('.editable')
    var person = new Object();
    for (var i = 0; i < editables.length; i++) {
        var editable = editables[i];
        person[editable.name] = editable.value;
    }
    var jform = JSON.stringify(person);
    $.post('/Person/EditPerson', jform, null, 'json');
}
6
  • Can you show the code from where you are posting the parameter "person"? Commented Dec 24, 2012 at 6:46
  • You mean the javascript? Commented Dec 24, 2012 at 6:49
  • Have you tried to use Html.BeginFormAjax() or so? What js does it generate? Commented Dec 24, 2012 at 7:03
  • I am not sure how to use Html.BeginFormAjax() and would prefer to not be locked into a form anyways. Eventually I will be using a foreach loop to generate a variable number of inputs and need to package the results of those inputs into an array. Commented Dec 24, 2012 at 7:09
  • Does your person object have the same properties as the personupdatemodel class? Commented Dec 24, 2012 at 8:34

1 Answer 1

1

Try using an AJAX post and specifying the
contentType to 'application/json; charset=utf-8'

     $.ajax( {
     type: "POST",
     url: /Person/EditPerson',
     contentType: 'application/json; charset=utf-8',
     data: jform
     }

If this does not solve the issue please post the header information. I am guessing that using the $post is sending "application/x-www-form-urlencoded" data which .NET MVC does not like.

http://api.jquery.com/jQuery.ajax/

Sign up to request clarification or add additional context in comments.

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.