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
            }
        });
    });
chokkaStudentAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

MishaProgrammerCommented:
If you want to pass data to controller, you can use Ajax (Jquerry):
$(document).ready(function () {
   $("button").click(function (){

        $.ajax({
            url: '/YourController/YourMethod',
            type: 'POST',
            data: this.id,
            success: function(result) {

            },
            error: function() {
            }
        });
    });
});

Open in new window

0
chokkaStudentAuthor Commented:
I am using HttpGet.  And not the Post ..

Do the syntax for Get and Post remains same ..

url: '/YourController/YourMethod',
0
MishaProgrammerCommented:
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
             data: JsonData,  
                type: "GET",

Open in new window

0
Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

chokkaStudentAuthor Commented:
I just need to get only the html input I'd. It's just one field value
0
chokkaStudentAuthor Commented:
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
0
MishaProgrammerCommented:
Ok, try to use this code:
//controller
[HttpGet]
public ActionResult ActionName(string id)
{
// do something
    return View();
}
// view
 href="/ControllerName/ActionName/ValueId"
--or
Url.Action("ActionName", "ControllerName", new { id = id });

Open in new window


Also you can use ViewData to pass some value to controller
ViewData["ID"] = YourId;
0
chokkaStudentAuthor Commented:
How do the Id parameter (string Id )is getting  passed to return value : View.

Can you please help me on that ..
0
MishaProgrammerCommented:
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)"
0
chokkaStudentAuthor Commented:
Here is my actual Syntax :

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")';
                
            }
        });
    });

Open in new window



From Controller :

[HttpGet]
        public ActionResult ActionName(string id)
        {

            var vm = id;
            return View(vm);
        }

Open in new window

0
chokkaStudentAuthor Commented:
still my ActionResult is not getting invoked.
0
MishaProgrammerCommented:
Try this. I has tested it, It works.
$('button').click(function () {
        var btn = $(this).attr('id');
        alert(btn);
        $.ajax({
            type: 'GET',
            url: '@Url.Action("ActionName", "TPreOpReminder")' + '?id=' + btn,
            success: function (result) {
                alert("good!");
            },
             error: function(data) {
                 alert("error!");
              }
      });
 });

Open in new window


it isn't necessary to write postfix "controller" when you call its method.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
chokkaStudentAuthor Commented:
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!");
            }

        });
    });
0
chokkaStudentAuthor Commented:
 $('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);
        }

Open in new window

0
chokkaStudentAuthor Commented:
error alert is firing
0
MishaProgrammerCommented:
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
 return View(vm);

Open in new window

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);

Open in new window

0
chokkaStudentAuthor Commented:
To test this.. I changed my syntax as follows..

I agree with you.  ActionName is not detecting

 
  [HttpGet]
        public IActionResult ActionName(string Id)
        {

            string vm = Id;
            return NotFound();
        }

Open in new window

0
chokkaStudentAuthor Commented:
Jquery syntax is giving correct url : https://localhost/MyController/ActionName?id=saveButton

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);
                }
            });
        });
    });

Open in new window



And there by ActionName is not getting invoke from MyController

   
[HttpGet]
        public IActionResult ActionName(string id)
        {
            Console.WriteLine(id);
            Console.ReadLine();

            return NotFound();
        }

Open in new window

0
MishaProgrammerCommented:
Did really  not getting invoke ? Did you place break point to this method and there is no handling it?
You place
 Console.ReadLine();

Open in new window

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.
0
chokkaStudentAuthor Commented:
Thank you !!
0
MishaProgrammerCommented:
Glad to help you!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.