AJAX & ASP.NET MVC 3: How to pass a string parameter AND an array at the same time as the controller

I have the following controller signature:

public void DoSomething(string dialerJob, MyViewModel[] agentStates) 

The presented models represent form fields in an array (selected elements in the HTML table). I figured out how to pass int form elements as an array argument to the controller thanks to Robert Koritnik.toDictionary () jQuery plug-in (http://erraticdev.blogspot.com/2010/12/sending-complex-json- objects to aspnet.html )

However, now I need to pass one additional parameter of the string (from the drop-down list) to the controller, and I cannot figure out how to do this. I tried various combinations, for example:

  var job = $('#DialerJobs').attr('value'); var data1 = $.toDictionary(data, "agentStates"); $.ajax({ url: "/Blending/ChangeOutboundJob", type: "POST", dataType: "application/JSON", data: {job, data1} }); 

I also tried the following:

  var job = $('#DialerJobs').attr('value'); var data1 = $.toDictionary(data, "agentStates"); $.ajax({ url: "/Blending/ChangeOutboundJob", type: "POST", dataType: "application/JSON", data: {dialerJob: job, agentStates: data1} }); 

But no one is working.

If I remove the dialerJob parameter from the data to send, the agents will be populated correctly in the controller. And what is sent looks like this:

 agentStates[0].agentId=7654&agentStates[0].projectId=999&agentStates[0].stateId=1&agentStates 

[0] .subStateId = 1 & agentStates [1] .agentId = 9876 & agentStates [1] .projectId = 999 & agentStates

[1] .stateId = 1 & agentStates [1] .subStateId = 1

But if I included dialerJob, then what is sent:

 dialerJob=SomeJob&agentStates[0][name]=[0].agentId&agentStates[0][value]=84&agentStates[1][name]= 

[0] .projectId & agentStates [1] [value] = 999 & agentStates [2] [name] = [0] .stateId & agentStates [2] [value]

= 1 & agentStates [3] [name] = [0] .subStateId & agentStates [3] [value] = 1 & agentStates [4] [name] = [1] .agentId & agentStates

[4] [value] = 15884 & agentStates [5] [name] = [1] .projectId & agentStates [5] [value] = 999 & agentStates [6] [name] = [1] .stateId & agentStates [6 ] [value] = 1 & agentStates [7] [name] = [1] .subStateId & agentStates [7] [value] = 1

That everything went bad ...

+4
source share
2 answers

You can use the JSON request:

 $.ajax({ url: '@Url.Action("ChangeOutboundJob", "Blending")', type: 'POST', contentType: 'application/json; charset=utf-8', data: JSON.stringify({ dialerJob: 'job', agentStates: [ { property1: 'value 1', property2: 'value 2' }, { property1: 'value 3', property2: 'value 4' } ] }), success: function (result) { // TODO: process the results } }); 

This will be successfully displayed for the following controller action:

 public void DoSomething(string dialerJob, MyViewModel[] agentStates) 

where MyViewModel is defined as follows:

 public class MyViewModel { public string Property1 { get; set; } public string Property2 { get; set; } } 

Note. The JSON.stringify method JSON.stringify built into all modern browsers. If you need to support legacy browsers, you need to include the json2.js script in your page.

+8
source

change the js job var, which will be called dialerJob, the names must match for matching to automatically match them.

0
source

Source: https://habr.com/ru/post/1369192/


All Articles