-3

I do not have idea how to pass the full list by the Ajax. So i have c# list in the cshtml file and I want to pass it to the controller via Ajax. I know how to Pass simple variable, but don't know how to pass full list of objects. there is a simple js script:

$.get("/home/function?List=" + SthList, function (r) {
    $("#Table").html(r);
});

It should not work, because SthList isn't SJ object. But when I want to use razor it does not work anyway.

There is a controller

public ActionResult function(List<Object> List)
{
    ...
    return PartialView(sth);
}

do you have any tips for me?

Blabla
  • 367
  • 4
  • 16
  • u can use json.net library. – Dheeraj Jul 25 '16 at 12:35
  • What is the model and its properties. And what is `SthList` –  Jul 25 '16 at 12:35
  • SthList is a c# List, it isn't really important what is the model. This questuion does not depend on this. – Blabla Jul 25 '16 at 12:58
  • Of course it does! –  Jul 25 '16 at 13:12
  • It would make more sense to put a list in a form and POST it. – Scott Hannen Jul 25 '16 at 13:16
  • The model in the view is a list of few model, because I need to pass few models to the view. Yes, I know that I can POST it, but I want to change table dynamically, and I have few List to pass to the controller, this is just simplyfied model what I want to achieve. The question is simple, how to pass c# list by the ajax? – Blabla Jul 25 '16 at 13:26

1 Answers1

1

First, you need to transform your C# Array into a JsArray. To do that you need to mix razor and javascript.

There is a question for how to do that ( how to convert c# array into a js array?) Mix Razor and JavaScript

The other problem is passing array via ajax to the controller.

There is another question for that. Post array to mvc controller

So you can find your answer with this questions.

Community
  • 1
  • 1
afrikaan
  • 425
  • 5
  • 21