How to treat an array with N lines and turn to a list to send to C#

Asked

Viewed 99 times

0

I’m sending an array of angular js for the controller of C#, and I’m treating him with stringfy for json.

Only in my way POST he gets nothing, just returns null

Code C#

 public void Post(string values)
    {
        var r = values;

    }

Javascript code

    $scope.SubmitData = function (DataToSubmit) {
    var string = JSON.stringify(DataToSubmit);
    $http({
        method: 'POST',
        data: {
            values: string,
        },
        url: 'api/aulamethod',
    });
}

Date which is inserted in Inserts

inserir a descrição da imagem aqui

data appearing on var string after the stringfy

 values:"[{"CompanyName":"column a","SupplierCode":"column b","DocumentNumber":"column c","Reference":"column d","InternalCurrencyValue":"column e","BlockadeUnblocking1":"column f","Justification":"column g","Request":"column h","$$hashKey":"object:23"}]"
  • Try it this way: data: { string }

  • not from :/ already tried

  • I believe he considers aulamethod as the method name in c#

  • In your Post method change your signature to (List<string> values).

  • yes I changed, I sent wrong pq forgot to change, already put aulamethod... I will try netinho m/

  • didn’t work tbm

  • What is the MVC version of your Webapi?

Show 2 more comments

1 answer

1


If your goal is to post this value as String, you must first ensure that you are performing your post with the type in application/json.

Create a Viewmodel to reflect the structure you’re posting:

public class ValuesViewModel
{
    public string Values { get; set; }
}

And in the controller don’t forget to add the [FromBody]

[HttpPost]
public void Post([FromBody]ValuesViewModel values)
{ 
   var r = values;       
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.