1

I am trying to generate a list with AJAX and post it to controller. Here is my code so far;

var objects = new Array();

here is a loop {
var object = {
            a: 1,
            b: 2,
            c: 3
        };

objects[i] = object;
i++;
}

 $.ajax({
        type: "POST",
        url: "/Controller/Poster",
        cache: false,
        dataType: 'json',
        contentType: 'application/json; charset=utf-8',
        data: JSON.stringify(objects),
        success: function (result) {
            alert("posted");
        }
    });

Am I following the right way till here?

And in back end I created a model class;

public class ObjectModel
{
    public int a {get; set;}
    public int b {get; set;}
    public int c {get; set;}
}

Now I am trying to get this object list to my controller. After a research I tried create a controller method that takes an object list as parameter;

 [HttpPost]
 public JsonResult Poster(List<ObJectModel> olist)
    { 
    }

But I can't define a list in my controller. What am I supposed to do from now? And should I make any changes in my code?

Thanks!

3
  • Did you try post without JSON.stringify just objects? Commented Jan 12, 2014 at 15:35
  • and add attribute [HttpPost] to Poster Commented Jan 12, 2014 at 15:36
  • default model binder can't process List<T>,you can create a model binder for your type Commented Jan 13, 2014 at 7:33

3 Answers 3

2

I added System.Collections namepace to my controller. And I can define List now. After I added [HttpPost] attribute to my method and make public my class properties now there isn't any problem. My main problem was the System.Collections namespace. I successfully passed my object list to my controller.

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

3 Comments

In future add compile errors to question. It's dificult to find problem without all information. Also try this jetbrains.com/resharper very useful and offer solution for this and many other issues.
I'm not able to mark your answer as solution, because question is not mine, but I make upvote, because you find solution for your problem, but thenn add more useful information.
Yes you're rihgt. I will provide more information in my future questions. Thank you for your advice about resharper. I will try it.
0

Try this

data: objects

instead of

data: JSON.stringify(objects)

Add attribute to Poster

[HttpPost]
public JsonResult Poster(List<ObJectModel> olist)

Change ObjectModel

public class ObjectModel
{
    public int a {get; set;}
    public int b {get; set;}
    public int c {get; set;}
}

4 Comments

I have already added HttpPost attr. to my method. So I still cant define a list as a parameter. I think my main problem is this. I can define IList, Arraylist etc but can't List.
did you change property to public?
do you return something from Poster?
First of all thank you for your all effort. I have resolved my problem. Please read my own answer.
0

try

data: $.param(objects),

insted of

data: JSON.stringify(objects),

2 Comments

what the difference, why did you copy two times the same string from my answer?
sorry simple copy paste mistake see the updated answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.