A
A
Artem2014-04-11 11:10:12
JavaScript
Artem, 2014-04-11 11:10:12

Send collection of data to ASP.NET MVC Action via jQuery post to different domain?

Hey !
I created an index.html file on the C:\ drive, then in the file I wrote a simple post request via $.post() that sends an array with data to the url: localhost:1901/Test/AddWords . On the ASP.NET MVC server, an application that runs locally url: localhost:1901 on the AddWords action of the Test controller.
In action, the array comes only with empty element fields.

Code from html file.
The file is just in a folder on the computer.

//index.html

$.post("http://localhost:1901/Test/AddWords",{words:words},function(data){
});


A collection comes to the server with all elements and only all properties are null.

Server code.
public ActionResult AddWords(Word [] words)
{
    //words[0].Translation = null;
}


This is how the data is passed in the request.
31235374d4da4caa99aadcb810b9f063.PNG

PS Thanks in advance!

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Artem, 2014-04-11
@Thugmaster

I redid it like this

public ActionResult AddWords()
{
            var wordsModel = HttpContext.Request.Form["words"];
            var wordArray = new JavaScriptSerializer().Deserialize<WordModel[]>(wordsModel);
}

Code on the page.
But I don't like this code!!

R
Roman Pavlov, 2014-04-11
@RomanPavlov

The fields are empty because the model binding does not work. Domains have nothing to do with it.
It looks terrible, but I think the essence is clear:

$.post("http://localhost:1901/Test/AddWords", {
            "words[0].id" : 123,
            "words[0].EngValue" : 123123,
            "words[0].Translation" : 123123,
        }, function (data) {
    });

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question