1

How can i send both serialize object and string array to asp.net controller? My sample ajax is here=>

var $this = $("#FormName");
var frmValues = $this.serialize();
var sortorder = [];
$(".StepOrder").each(function (index) {
   sortorder.splice(index, 0, ($(this).attr('id')));
});                           

$.ajax({
  type: 'POST',
  traditional:true,
  url: '/MyController/MyAction',
  datatype:'html',
  contentType: 'application/json; charset=utf-8',
  data:JSON.stringify( { frmValues: frmValues, sortorder: sortorder }),
  success: function (data) {

  },
});

My Controller=>

[HttpPost]
public ActionResult MyAction(MyModel frmValues,List<string> sortorder)
{
 return View();
}

MyModel and sortorder in controller are always null.I also try like=>

$.ajax({
 type: 'POST',
 url: '/MyController/MyAction',
 data: { frmValues: frmValues, sortorder: sortorder },
 success: function (data) {

 },
});

It also don't work.I don't known why it doesn't work.

5
  • try JSON.stringify({ frmValues: frmValues, sortorder: sortorder }) Commented Apr 24, 2017 at 11:28
  • I already try like that.It also don't work.:( Commented Apr 24, 2017 at 11:29
  • can you add view and MyModel in the question Commented Apr 24, 2017 at 11:30
  • and also check the network tab in browser to check the request parameters Commented Apr 24, 2017 at 11:33
  • change datatype to json Commented Apr 24, 2017 at 14:45

1 Answer 1

3

Using this approach, create a DTO class which the controller action accepts as request:

$.ajax({
 type: 'POST',
 url: '/MyController/MyAction',
 data: { frmValues: frmValues, sortorder: sortorder },
 success: function (data) {

 },
});

Then DTO Class as below, make sure the keys from payload match the property name.

public class ExampleDTO 
{
   public MyModel frmValues {get; set;}
   public List<string> sortorder {get; set;}
}

Use it as:

[HttpPost]
public ActionResult MyAction(ExampleDTO req)
{
  //var frmValues = req.frmValues;
  //string sortOrder = req.sortorder;
  return View();
}
Sign up to request clarification or add additional context in comments.

1 Comment

It makes no difference it the parameters are in the method or in a model. This does nothing to solve OP's problem

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.