1

For unknown reason for me data received by my function in controller is always null. My data is translated into json properly. Data is send but not received (sending ends with success).

Here You can see my Ajax function

$.ajax({
    url: "/Devices/Read",
    contentType: "text",
    type: "POST",
    datatype: "json",
    data: ko.toJSON(self),
    error: function (xmlHttpRequest, errorText, thrownError) {
        console.log("error")
    },
    success: function (data) {
        if (data != null) {
            console.log("success");
        }
    }
});

here my function in controller:

[HttpPost]
public ActionResult Read(string data)
{
    return Json(new Object());
}

I tried:

  1. changing parameter type to: Object, String
  2. changing contentType to : text, application/json; charset=utf-8

Still no efects. Can anyone suggest me where should I search for mistake?

UPDATE: After changing method declaration to:

public ActionResult Read(object[] data, string DeviceUser, string DeviceId, string number)

Last three strings are OK but first object consist of proper number of elements but they are null. in JS they are alements in array of classes

structure of my JS objects:

  var Eq = function (name, price) {
    var self = this;
    self.DeviceSerialNumber = ko.observable();
    self.Batch = ko.observable();

};
var ViewModel = function () {
    var self = this;
    self.data = ko.observableArray([]);
    self.DeviceUser = ko.observable();
    self.DeviceId = ko.observable();
    self.number = ko.observable(1);
};
szpic
  • 4,346
  • 15
  • 54
  • 85
  • You should use the getJSON function from jQuery. This automaticly parses the json data. http://api.jquery.com/jquery.getjson/ – Jerodev Jan 21 '14 at 08:33
  • using the `Json()` method from the controller to return your JSON will automatically set the content type, so you don't need to worry about setting that. Your method currently returns an empty object, so I'm not sure what you're expecting? Can you edit your OP to include the actual code you're having the issue with? – Adrian Thompson Phillips Jan 21 '14 at 10:22

3 Answers3

1

In your controller action, why do you:

return RedirectToAction("Index"); 

I think you may want to:

return Json(data);
engineforce
  • 2,840
  • 1
  • 23
  • 17
  • At this moment it is just a mock-up. In future this function will call method which will add received data to database and after adding it redirect user to main page. But this redirect doesn't work either ;) – szpic Jan 21 '14 at 08:52
  • In your ajax call, you specified 'datatype: json', your controller action must return JSON. Otherwise you should specify 'datatype: html'. – engineforce Jan 21 '14 at 09:20
  • OK. I edited it. But it isn't point of my question. My asking why data received by me is in half empty. – szpic Jan 21 '14 at 09:30
  • Ok now I understand your question. Please refer to http://stackoverflow.com/questions/3168401/json-formatting-sending-json-via-jquery-ajax-post-to-java-wicket-server. The key is to use JSON.stringify() and set the correct contentType – engineforce Jan 21 '14 at 09:57
  • Checked this question. JSON.Stringify of my Js object returns empty string and ko.ToJSON is native stringify for Knockoutjs. So problem isn't in this place – szpic Jan 21 '14 at 10:06
  • What is your JS object look like? – engineforce Jan 21 '14 at 10:11
1

If you are writing an HTTP Post method, then the redirect to action does not really make sense, because you are posting data, not requesting a page. You could do something like this:

[HttpPost]
public ActionResult Read(string Data)
{
    return Json(Data);
}
1

OK problem was in setting proper parameter type in method.

TO solve my problem I need to modify method declaration to:

public ActionResult Read(ViewModel m)

and add ViewModel like this:

  public class ViewModel
{
    public List<InnerClass> data { get; set; }
    public string DeviceUser  {get;set;}
    public string DeviceId {get;set;}
    public int number {get;set;}

}
public class InnerClass
{
    public string DeviceSerialNumber { get; set; }
    public string Batch { get; set; }
}

now all data is received.

Thanks You all for help and suggestions. I will give you all +1 :)

szpic
  • 4,346
  • 15
  • 54
  • 85