如何将参数从 Ajax 函数传递给控制器​​动作?

我的视图中有一个按钮,它调用一个 jQuery Ajax 函数,从我的模型中传入参数


<input type="button" value="Run Check" onclick="runCheck('@actionItem.StepID', '@Model.Client.DatabaseConnectionString', '@Model.Client.ClientID')" />

jQuery 函数


<script type="text/javascript">

        function runCheck(x, y, z) {

            $.ajax({

                url: '@Url.Action("ProcessFeedbackHasRows", "Client")',

                type: 'POST',

                contentType: 'application/json;',

                data: { stepId: x, databaseConnectionString: y, clientId: z },

                success: function (data) {

                    if (data.IsValid) {

                        //alert('true');

                        var url = '@Url.Action("ViewProcessingFeedBackPartial", "Client")';

                        $("#processingFeedbackPartialDiv").load(url, { stepId, databaseConnectionString, clientId },

                            function () {

                                $("#confirmButton").removeAttr("style");

                            });

                    } else {

                        //alert('false');

                        var newUrl = '@Url.Action("Processing", "Client")';

                        window.location = newUrl;

                    }

                }

            });

    };

</script>

最后我的控制器动作


public JsonResult ProcessFeedbackHasRows(int StepId, string DatabaseConnectionString, int ClientID)

    {

        bool isValid = true;

        FeedbackDetails feedbackDetails = new FeedbackDetails();


        feedbackDetails.Data = _clientProcessingService.GetProcessingFeedbackDetails(StepId, DatabaseConnectionString);


当我在控制器中硬编码特定值以表示适当的步骤、客户端和数据库时,ajax 函数中的逻辑起作用,但是当我调试时,我看到两个整数 as0和字符串 as null。


如何将这些值传递给控制器?我考虑过将它们存储在ViewBagor 中,ViewData但这看起来很笨重,并不是一个很好的做法。


一只斗牛犬
浏览 190回答 2
2回答

回首忆惘然

尝试这个,var req={ stepId: x, databaseConnectionString: y, clientId: z }function runCheck(x, y, z) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $.ajax({&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; url: '@Url.Action("ProcessFeedbackHasRows", "Client")',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; type: 'POST',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; contentType: 'application/json;',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; data: JSON.stringify(req),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; success: function (data) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (data.IsValid) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //alert('true');&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var url = '@Url.Action("ViewProcessingFeedBackPartial", "Client")';&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $("#processingFeedbackPartialDiv").load(url, { stepId, databaseConnectionString, clientId },&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; function () {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $("#confirmButton").removeAttr("style");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //alert('false');&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var newUrl = '@Url.Action("Processing", "Client")';&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; window.location = newUrl;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; };

墨色风雨

根据这个问题,我必须删除我的contentType财产并且成功传递了值。<script type="text/javascript">&nbsp; &nbsp; function runCheck(x, y, z) {&nbsp; &nbsp; &nbsp; &nbsp; $.ajax({&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; url: '@Url.Action("ProcessFeedbackHasRows", "Client")',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; type: 'POST',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; data: { stepId: x, databaseConnectionString: y, clientId: z },&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; success: function (result) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (result.IsValid) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; alert('true');&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var url = '@Url.Action("ViewProcessingFeedBackPartial", "Client")';&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $("#processingFeedbackPartialDiv").load(url, { stepId, databaseConnectionString, clientId },&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; function () {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $("#confirmButton").removeAttr("style");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; alert('false');&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var newUrl = '@Url.Action("Processing", "Client")';&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; window.location = newUrl;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; });};
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript