$ .ajax方法不调用控制器操作方法

时间:2022-12-01 23:16:38

I am calling javascript function on @Html.ActionLink clicked event. The Javascript function calls the jquery function in which I have written the $.ajax method for calling the json controller method. But it is not calling the controller method.......

我在@ Html.ActionLink点击事件上调用了javascript函数。 Javascript函数调用jquery函数,我在其中编写了$ .ajax方法来调用json控制器方法。但它没有调用控制器方法.......

Here is my code:

这是我的代码:

View

@Html.ActionLink("Submit", "AdminEvaluate", "Application", new { onclick = "Show('" + Model.applicationList.ApplicationId + "','statusList')" })|

Here, ApplicationId is coming from database and statusList is Radio Button group name from where I need to find the selected radio button.

这里,ApplicationId来自数据库,而statusList是单选按钮组名,我需要从中找到所选的单选按钮。

Javascipt and jQuery code

Javascipt和jQuery代码

function Show(appId, appStatus) {
    $.fn.gotof(appId,appStatus);
}

Getting parameter from view and passing to jQuery function

从视图获取参数并传递给jQuery函数

jQuery function

$(document).ready(function () {
    $.fn.gotof = function (appId, appStatus) {
        var selectedItem = $("input[name='" + appStatus + "']:checked").val();  
        $.ajax({
            url: '/Application/UpdateApplicantStatus',
            data: { id: appId , status: selectedItem},
            traditional: true,
            dataType: "json",
            contentType: "application/json",
            success: function (data) {
                alert(data);
            }
        });
    });

Controller Method

public JsonResult UpdateApplicantStatus(int id, string status){
    return Json("Correct", JsonRequestBehavior.AllowGet);
}   

I have put break point on controller method but it is not coming at a break point.

我已经在控制器方法上设置了断点,但它没有在断点处出现。

Thanks in Advance.

提前致谢。

5 个解决方案

#1


2  

May be this sample help you :) let me know

可能是这个样本帮助你:)让我知道

ajax call with type :

ajax调用类型:

 $.ajax({
            type: "GET",
            url: '@Url.Action("UpdateApplicantStatus", "Application")',
            contentType: "application/json; charset=utf-8",
            data: { id: appId , status: selectedItem },
            dataType: "json",
            success: function() { alert('Success'); }

            });

controller :

public ActionResult UpdateApplicantStatus(int id, string status){
    return Json(// blah blah blah ...
}  

This should work else let me know

这应该工作,让我知道

#2


1  

Your ajax call is wrong.

你的ajax电话是错误的。

If your controller method signature is

如果您的控制器方法签名是

public JsonResult UpdateApplicantStatus(int id, string appStatus)

Then you MUST have parameter names matching with your parameters of your ajax call.
So, instead of

然后你必须有参数名称与你的ajax调用参数匹配。所以,而不是

data: { id: appId , status: selectedItem}

Just write

data: { id: appId , appStatus: selectedItem}

If you have any other problem, just post the error(s) you should have in your browser's console.

如果您有任何其他问题,只需在浏览器控制台中发布您应该有的错误。

#3


0  

Maybe parameter names of controller function and ajax calling function should match.

也许控制器函数和ajax调用函数的参数名称应匹配。

you can try this

你可以试试这个

data: { id: appId , appStatus: selectedItem},

data:{id:appId,appStatus:selectedItem},

#4


0  

Do that way actually 3 mistakes 1 was notify by @Mr Code the type is missing 2nd your parameters not match as your function takes one you send the data in wrong format..

这样做实际上有3个错误1被@Mr代码通知类型丢失第2个你的参数不匹配,因为你的函数需要你以错误的格式发送数据。

$(document).ready(function () {
    $.fn.gotof = function (appId, appStatus) {
        var selectedItem = $("input[name='" + appStatus + "']:checked").val();  
        $.ajax({
            url: '/ApplicationController/UpdateApplicantStatus',
data:"{'id':'" + appId + "', 'appStatus': '" + selectedItem+ "'}",//here you format is wrong i correct it
            traditional: true,
            dataType: "json",
            contentType: "application/json",
            success: function (data) {
                alert(data);
            }
        });
    });

#5


0  

sat on this for hours:

坐了几个小时:

When using ajax - json in a js.script file:

the url is written with its real path e.g. -  url: '/contoller/Action',


                                $.ajax({
                                   url: '/NextPage/GetSpDropDown',                                      
                                   data: {'segInputVal': segInputVal}, 
                                    cache: false, 
                                    type: "GET",
                                    dataType: "json",
                                    success: function (result) {
                                     if (result.Success) {
                                     var resultArray = result.Result;.....

If the script is written in your HTML page then you must use

如果脚本是在HTML页面中编写的,那么您必须使用

@(Url.Action(Action,controller))

                                   $.ajax({
                       url: "@(Url.Action("GetSpDropDown", "NextPage"))",
                                    data: { 'segInputVal': segInputVal },
                                    cache: false,
                                    type: "GET",
                                    dataType: "json",
                                    success: function (result) {
                                     if (result.Success) {
                                     var resultArray = result.Result;....

And dont forget not to include the word controller in your controller name.

并且不要忘记不在控制器名称中包含单词controller。

I have had SO MUCH help from *- hope to pay a little back

我从*得到了很多帮助 - 希望能够支付一些回报

#1


2  

May be this sample help you :) let me know

可能是这个样本帮助你:)让我知道

ajax call with type :

ajax调用类型:

 $.ajax({
            type: "GET",
            url: '@Url.Action("UpdateApplicantStatus", "Application")',
            contentType: "application/json; charset=utf-8",
            data: { id: appId , status: selectedItem },
            dataType: "json",
            success: function() { alert('Success'); }

            });

controller :

public ActionResult UpdateApplicantStatus(int id, string status){
    return Json(// blah blah blah ...
}  

This should work else let me know

这应该工作,让我知道

#2


1  

Your ajax call is wrong.

你的ajax电话是错误的。

If your controller method signature is

如果您的控制器方法签名是

public JsonResult UpdateApplicantStatus(int id, string appStatus)

Then you MUST have parameter names matching with your parameters of your ajax call.
So, instead of

然后你必须有参数名称与你的ajax调用参数匹配。所以,而不是

data: { id: appId , status: selectedItem}

Just write

data: { id: appId , appStatus: selectedItem}

If you have any other problem, just post the error(s) you should have in your browser's console.

如果您有任何其他问题,只需在浏览器控制台中发布您应该有的错误。

#3


0  

Maybe parameter names of controller function and ajax calling function should match.

也许控制器函数和ajax调用函数的参数名称应匹配。

you can try this

你可以试试这个

data: { id: appId , appStatus: selectedItem},

data:{id:appId,appStatus:selectedItem},

#4


0  

Do that way actually 3 mistakes 1 was notify by @Mr Code the type is missing 2nd your parameters not match as your function takes one you send the data in wrong format..

这样做实际上有3个错误1被@Mr代码通知类型丢失第2个你的参数不匹配,因为你的函数需要你以错误的格式发送数据。

$(document).ready(function () {
    $.fn.gotof = function (appId, appStatus) {
        var selectedItem = $("input[name='" + appStatus + "']:checked").val();  
        $.ajax({
            url: '/ApplicationController/UpdateApplicantStatus',
data:"{'id':'" + appId + "', 'appStatus': '" + selectedItem+ "'}",//here you format is wrong i correct it
            traditional: true,
            dataType: "json",
            contentType: "application/json",
            success: function (data) {
                alert(data);
            }
        });
    });

#5


0  

sat on this for hours:

坐了几个小时:

When using ajax - json in a js.script file:

the url is written with its real path e.g. -  url: '/contoller/Action',


                                $.ajax({
                                   url: '/NextPage/GetSpDropDown',                                      
                                   data: {'segInputVal': segInputVal}, 
                                    cache: false, 
                                    type: "GET",
                                    dataType: "json",
                                    success: function (result) {
                                     if (result.Success) {
                                     var resultArray = result.Result;.....

If the script is written in your HTML page then you must use

如果脚本是在HTML页面中编写的,那么您必须使用

@(Url.Action(Action,controller))

                                   $.ajax({
                       url: "@(Url.Action("GetSpDropDown", "NextPage"))",
                                    data: { 'segInputVal': segInputVal },
                                    cache: false,
                                    type: "GET",
                                    dataType: "json",
                                    success: function (result) {
                                     if (result.Success) {
                                     var resultArray = result.Result;....

And dont forget not to include the word controller in your controller name.

并且不要忘记不在控制器名称中包含单词controller。

I have had SO MUCH help from *- hope to pay a little back

我从*得到了很多帮助 - 希望能够支付一些回报