Reputation: 27011
How to create a method in the Controller so that it becomes AJAX Callable?
For example, code {0} can be accessed using {1} when the return type is ActionResult:
{0}:
public ActionResult TestWithActionResult(string id)
{
return View();
}
{1}:
http://localhost:4574/ControllerName/TestWithActionResult/2
but my below code {3} can't be accessed with {4}:
{3}:
public string TestWithString(string id)
{
return "some string";
}
{4}:
http://localhost:4574/ControllerName/TestWithString/2
the id in the {3} is always null when I open {4}.
Should I decorate {3} differently? How?
Thanks,
Upvotes: 0
Views: 381
Reputation: 28699
Your method needs to return an ActionResult
of some kind (or ViewResult
, JsonResult
, etc) to be considered an Action. You could just use a JsonResult
to write the text to the Response.
public ActionResult TestWithActionResult(string id)
{
return new Json("some string");
}
Upvotes: 0
Reputation: 4702
Try using a JsonResult:
public JsonResult TestWithActionResult(string id)
{
return Json("Some string");
}
Edit:
You can call the function using AJAX like this (using Jquery)
$.ajax(
url: 'http://localhost/TestWithActionResult/feed1',
type:'GET',
dataType: 'json',
success: function(data) {
//Process data
},
error: function(error) { }
);
Upvotes: 1