chokka
asked on
ActionResult in the Controller is not invoking?
ASP.Net MVC 5
I am trying to fetch the result from Jquery to my controller.
However, in my controller - ActionResult is not invoking. What could be the reason ?
[HttpGet]
public ActionResult ActionName(string id)
{
// do something
var vm = id;
return View(vm);
}
JQuery Syntax :
$('button').click(function () {
var btn = $(this).attr('id');
alert(btn);
$.ajax({
type: 'GET',
url: '@Url.Action("ActionName", "ControllerName")',
data: { id: btn },
success: function (result) {
// do something
}
});
});
I am trying to fetch the result from Jquery to my controller.
However, in my controller - ActionResult is not invoking. What could be the reason ?
[HttpGet]
public ActionResult ActionName(string id)
{
// do something
var vm = id;
return View(vm);
}
JQuery Syntax :
$('button').click(function
var btn = $(this).attr('id');
alert(btn);
$.ajax({
type: 'GET',
url: '@Url.Action("ActionName",
data: { id: btn },
success: function (result) {
// do something
}
});
});
ASKER
I am using HttpGet. And not the Post ..
Do the syntax for Get and Post remains same ..
url: '/YourController/YourMetho d',
Do the syntax for Get and Post remains same ..
url: '/YourController/YourMetho
If you pass a lot of data, It may be a good idea to convert your action method to a HttpPost action method and use jQuery post to post data to that. GET has limitation on the query string value.
And try to set contentType and datatype
And try to set contentType and datatype
data: JsonData,
type: "GET",
ASKER
I just need to get only the html input I'd. It's just one field value
ASKER
Can you please give me the syntax for both Controller and Jquery. I have the Jquery syntax. Let me have your syntax for Controller on invoking this Jquery function
Ok, try to use this code:
Also you can use ViewData to pass some value to controller
ViewData["ID"] = YourId;
//controller
[HttpGet]
public ActionResult ActionName(string id)
{
// do something
return View();
}
// view
href="/ControllerName/ActionName/ValueId"
--or
Url.Action("ActionName", "ControllerName", new { id = id });
Also you can use ViewData to pass some value to controller
ViewData["ID"] = YourId;
ASKER
How do the Id parameter (string Id )is getting passed to return value : View.
Can you please help me on that ..
Can you please help me on that ..
When you use Url.Action("ActionName", "ControllerName", new { id = id }); you call method with name "ActionName" in conrtoller with name "ControllerName" and pass parameter "id", because you method in cotroller expect to get this parametr "ActionName(string id)"
ASKER
Here is my actual Syntax :
Name of the Controller : TPreOpReminderController
Name of the method ActionResult : ActionName
From Jquery :
From Controller :
Name of the Controller : TPreOpReminderController
Name of the method ActionResult : ActionName
From Jquery :
$('button').click(function () {
var btn = $(this).attr('id');
alert(btn);
$.ajax({
type: 'GET',
url: '@Url.Action("ActionName", "TPreOpReminderController",new { id = id })',
data: { id: btn },
success: function (result) {
//window.location = '@Url.Action("AnotherAction", "BaseController")';
}
});
});
From Controller :
[HttpGet]
public ActionResult ActionName(string id)
{
var vm = id;
return View(vm);
}
ASKER
still my ActionResult is not getting invoked.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Hi Misha..
I am using ASP.Net Core. I did not mention the Data Type : JSON.
When i run this syntax. I am getting the alert : error .
Even with Json, i am getting the error alert.
So there is something mismatch with ActionName, Controller ..
$('button').click(function () {
var btn = $(this).attr('id');
alert(btn);
$.ajax({
type: 'GET',
url: '@Url.Action("ActionName", "TPreOpReminder")',
data: { id: btn },
success: function (result) {
alert("good");
},
error: function (data) {
alert("error!");
}
});
});
I am using ASP.Net Core. I did not mention the Data Type : JSON.
When i run this syntax. I am getting the alert : error .
Even with Json, i am getting the error alert.
So there is something mismatch with ActionName, Controller ..
$('button').click(function
var btn = $(this).attr('id');
alert(btn);
$.ajax({
type: 'GET',
url: '@Url.Action("ActionName",
data: { id: btn },
success: function (result) {
alert("good");
},
error: function (data) {
alert("error!");
}
});
});
ASKER
$('button').click(function () {
var btn = $(this).attr('id');
alert(btn);
$.ajax({
type: 'GET',
url: '@Url.Action("ActionName", "TPreOpReminderUpperExtremityController")',
data: { id: btn },
success: function (result) {
alert("good");
},
error: function (data) {
alert("error!");
}
});
});
[HttpGet]
public IActionResult ActionName(string Id)
{
var vm = Id;
return View(vm);
}
ASKER
error alert is firing
If I correct understand you, your problem is that management doesn't get your controller method "ActionName".
In our previous this problem really was .
Put break point in method ActionName and you will be convinced if this problem still exists or not.
You get alert error, because there is no view, which ActionName can return
Also you can call another view with this code:
In our previous this problem really was .
Put break point in method ActionName and you will be convinced if this problem still exists or not.
You get alert error, because there is no view, which ActionName can return
return View(vm);
Click right button on ActionName method in MS Visual Studio and click "Add view". You can create view, which will be returned by this method,Also you can call another view with this code:
return View("NameOfView", Model);
ASKER
To test this.. I changed my syntax as follows..
I agree with you. ActionName is not detecting
I agree with you. ActionName is not detecting
[HttpGet]
public IActionResult ActionName(string Id)
{
string vm = Id;
return NotFound();
}
ASKER
Jquery syntax is giving correct url : https://localhost/MyController/ActionName?id=saveButton
However, Jquery function is throwing error as
object Object.
And there by ActionName is not getting invoke from MyController
However, Jquery function is throwing error as
object Object.
$(document).ready(function () {
$("button").click(function () {
var btn = $(this).attr('id');
alert(btn);
$.ajax({
url: '/MyController/ActionName',
type: 'GET',
data: { id: btn },
success: function (result) {
alert("good");
},
error: function (data) {
alert(data);
}
});
});
});
And there by ActionName is not getting invoke from MyController
[HttpGet]
public IActionResult ActionName(string id)
{
Console.WriteLine(id);
Console.ReadLine();
return NotFound();
}
Did really not getting invoke ? Did you place break point to this method and there is no handling it?
You place
Your controller expect console key. That`s why you dont get "error" or "good" alert!!!
There is no need to use Console class in this case.
You place
Console.ReadLine();
in your controller code.Your controller expect console key. That`s why you dont get "error" or "good" alert!!!
There is no need to use Console class in this case.
ASKER
Thank you !!
Glad to help you!
Open in new window