2

So I do Ajax and then I have this piece of code (JavaScript):

document.getElementById("HidenLink").style.visibility = "visible";
document.getElementById("HidenLink").innerHTML = "I have found " + data.pocetfound + " cases";
var arrStr = encodeURIComponent(JSON.stringify(data.listfound));
var Path = " ";
switch ('@ViewBag.TableName') {
         case "MYCASE":
              Path = "PATJ/PATHH"
         break;
       }
document.getElementById("HidenLink").href = Path + '/IDArr?array=' + arrStr;

Then I expect to get int[] IDArr in my Controller:

[HttpGet]
public async Task<IActionResult> MYRIGHTPATH(int? id, int[] IDArr)
{
  try {
    /*TESTS*/
     foreach (int i in IDArr) //Does not get executed
      {
         Console.WriteLine(i);
      }
        /*TESTS*/
   }catch (Exception e) {
                return RedirectToAction("Error", new { error = e });
            }
        }

My endpoint:

app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllerRoute(
                    name: "default",
                    pattern: "{controller=Home}/{action=Index}/{id?}");
                endpoints.MapRazorPages();
            });

My problem is that controller does not get array I am trying to send.

Thanks for help!

1
  • change , int[] IDArr to string Commented Jun 16, 2020 at 8:53

1 Answer 1

3

expect to get int[] IDArr in my Controller

[HttpGet]
public async Task<IActionResult> MYRIGHTPATH(int? id, int[] IDArr)`

To achieve your requirement, you can pass the data through query string like below.

?IDArr=1&IDArr=2

Or

?IDArr[0]=1&IDArr[1]=2

Test Result

enter image description here

For more information about model binding, please check this doc: https://learn.microsoft.com/en-us/aspnet/core/mvc/models/model-binding?view=aspnetcore-3.1#collections

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.