O
O
Omlet012017-06-04 20:50:45
JavaScript
Omlet01, 2017-06-04 20:50:45

ASP.NET How to pass array from javascript to controller?

I am writing an ASP.NET MVC application. The view has a script, it has an array
var array = [];
Question: How to pass this array to the controller method?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
mletov, 2017-06-04
@Omlet01

1) On the client, serialize the passed data to json
2) Create a class in C# similar to the structure of the array element generated by json, for example, SomeClass.
3) Create an action in the controller that receives a sheet of this class as input

public ActionResult MyAction(List<SomeClass> data)
{
          ....
}

4) Pass data to this controller action with ajax. I
recommend using jquery / angular, and not bother with pure js, depending on what you use on the client.
PS Do not forget to check the transfer method and the method specified in the Annotation of the action (POST, GET, PUT, DELETE)
PPS In your case, perhaps everything is simpler and you can receive actions as input
public ActionResult MyAction(string[] data)
{
          ....
}

Depends on what the array structure is. What does it contain: numbers, strings, objects?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question