Don Thomas Boyle
Don Thomas Boyle

Reputation: 3045

Ajax Post not sending data Correctly

VB.net, MVC4, asp.net views.

I would like to know why I cannot send or what I am doing wrong sending a parameter(int), parameter(model).

data:

var ID = '<%: Model.ID%>';
var data = $('#dlg').find('form').serialize();

Ajax Post:

$.ajax({
  url: '<%: Url.Action("EST", "Now")%>',
  type: 'POST',
  data: { id: ID, model: data },
  success: function (rData) {
    $('#divE').html(rData);
  }
});
 

Now - Controller:

<HttpPost> _
    Function EST(id As Integer, model As EViewModel) As ActionResult

So I'm passing a modelID and the forms data, sending them separately( with modifications ) both work, but sending them together it seems to null the 2nd parameter. Found by debugging the action.

Upvotes: 0

Views: 57

Answers (1)

Samuel
Samuel

Reputation: 1159

try use JSON.stringify. Include contentType: "application/json; charset=utf-8", when you omit the contentType value default is 'application/x-www-form-urlencoded; charset=UTF-8' this could become a trouble when you will post data

 $.ajax({
      url: '<%: Url.Action("EST", "Now")%>',
      type: 'POST',
      data: JSON.stringify({ id: ID, model: data }) ,
      contentType: "application/json; charset=utf-8",
      success: function (rData) {
        $('#divE').html(rData);
      }
    });

Upvotes: 1

Related Questions