1

I'm having some problems making an ajax call to receive a database object. I am making the ajax call in the View to pass a value from one of the HTML elements in the view to a method in my controller, which uses the value to search for and return an existing data record.

My ajax call:

$.ajax({
  url: "/MyController/MyFunction/",
  data: {Value: myvalue},
  type: 'GET',
  success: function (result) {
      //do something...
  }

My controller method:

 Function MyFunction(Value as String) As MyClass
   Dim record = SearchFunction(Value)

   Return record
 End Function

My data class:

Public Class MyClass

  Property Name As String
  Property Age As Integer
  Property DOB As Date
End Class

The Problem I am having is that the 'result' being received in the call returns a string with my model name, i.e. "MyClass", even though the controller is working fine and 'record' is successfully set as the correct data record.

I have tried various different potential solutions after scouring the internet including different dataTypes in the ajax call and different ajax calls like $.getJSON() instead but nothing's worked so far.

BenMorel
  • 34,448
  • 50
  • 182
  • 322
SenTaiyou
  • 129
  • 2
  • 10

1 Answers1

1

You need to convert your object to JSON before you give it back to the client.

return Json(record);

I can't test it right now, so I do not know if it will parse whole object automatically or you need to do the mapping manually.

Btw: There is also a JsonResult type that you might need to use instead of ActionResult
Edit: I just noticed you are not using ActionResult there. I do not know VB, so I am not sure if your syntax is correct. You might definitely try to return JsonResult instead of string (that might be also the part of a problem).

Btw2: I would use POST in your ajax call instead :)

Damb
  • 14,410
  • 6
  • 47
  • 49
  • Hi, Dampe, thanks for the feedback and i'll try your suggestions ASAP. As for the ActionResult...it's not a syntax error, I don't want to return a view with the posted data, just need the record back so I can use it on the current view I am working on. I have used a similar call in the same view, which worked but I was only using it to get back a single string or bool value. in which case the function would look like Function 'MyFunction(Value as String) As String' – SenTaiyou Apr 11 '13 at 15:29
  • Any details? Error? Where does it fail? Also check the similar questions ;) e.g. you can see the manual mapping to json here: http://stackoverflow.com/questions/227624/asp-net-mvc-controller-actions-that-return-json-or-partial-html – Damb Apr 11 '13 at 15:38
  • If I set the function type to 'JsonResult' the ajax call can't find the request. Also, both ActionResult and JsonResult would return a new view. I am able to get this to work by using an ActionLink and passing the model in a strongly types view but that's not what I want to do here. Need to get back the record so I can use it on the existing page without navigating away. – SenTaiyou Apr 11 '13 at 15:48
  • ActionResult does not 'return a view' always. ActionResult can be anything that is returned from an Action method such as a ContenrResult, FileResult, RedirectResult, JsonResult, ViewResult or a PartialViewResult. You have to have your return type as ActionResult or JsonResult, you need to Json encode your return class as @Dampe has mentioned. Lastly, you should preface your function with the Public identifier. Public Function MyFunction(byval Value as string) as ActionResult – Tommy Apr 11 '13 at 16:00
  • Thanks for the help Tommy, I'll give that a go first chance I get :) – SenTaiyou Apr 11 '13 at 16:25
  • Ok, being the idiot I am I had a couple of annoying syntax errors when I was setting up your suggestions....sorted that out and works fine now....thanks for the help! – SenTaiyou Apr 12 '13 at 09:55