Link to home
Start Free TrialLog in
Avatar of chokka
chokkaFlag for United States of America

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
            }
        });
    });
Avatar of Misha
Misha
Flag of Russian Federation image

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

Avatar of chokka

ASKER

I am using HttpGet.  And not the Post ..

Do the syntax for Get and Post remains same ..

url: '/YourController/YourMethod',
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

Avatar of chokka

ASKER

I just need to get only the html input I'd. It's just one field value
Avatar of chokka

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:
//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;
Avatar of chokka

ASKER

How do the Id parameter (string Id )is getting  passed to return value : View.

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)"
Avatar of chokka

ASKER

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

Avatar of chokka

ASKER

still my ActionResult is not getting invoked.
ASKER CERTIFIED SOLUTION
Avatar of Misha
Misha
Flag of Russian Federation image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of chokka

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

        });
    });
Avatar of chokka

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

Open in new window

Avatar of chokka

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

Avatar of chokka

ASKER

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

Avatar of chokka

ASKER

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

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.
Avatar of chokka

ASKER

Thank you !!
Glad to help you!