ASP。NET MVC 5從Javascript錯誤調用控制器方法

[英]ASP.NET MVC 5 calling controller method from Javascript Error


I am attempting to call a controller method from javascript and I seem to be having trouble getting this to execute correctly. I have very little experience with javascript and have followed other examples of how to do this from stackoverflow but I am still having some issues- if anyone can help that'd be fantastic.

我正在嘗試從javascript調用一個控制器方法,我似乎很難讓它正確執行。我幾乎沒有javascript的經驗,並且從stackoverflow上了解了其他的例子,但是我仍然有一些問題——如果有人能幫上忙的話,那就太棒了。

Basically what I am trying to do is set a .data tag on a javascript object to the string returned by a method on the controller (this method calls a webservice that runs a SQL Server function). The method needs to be passed one parameter which is used in the function.

基本上,我要做的是將javascript對象上的.data標記設置為控制器上的方法返回的字符串(該方法調用運行SQL Server函數的webservice)。該方法需要傳遞函數中使用的一個參數。

The code is below:

下面的代碼是:

Javascript Code

Javascript代碼

for (var i = 0; i < stats.length; i++)
{ 
    var stat = stats[i].data('id');
    var color = CallService(stat);
    this.node.fill = color;
}

JQuery Method

JQuery方法

    function CallService(id) {
    $.ajax({
        url: '@Url.Action("CallService", "NodeController")',
        type: 'GET',
        dataType: 'json',
        cache: false,
        data: { 'id': id },
        success: function (color) {
            return color;
        },
        error: function () {
            alert('Error occured');
        }
    });
} 

Controller Method

控制器方法

    [HttpGet]
    public JsonResult CallService(string id)
    {
        var idNum = Convert.ToInt32(id);
        var StationService = new getStationStatus.Service1SoapClient("Service1Soap");
        string color = StationService.getStationStatus(idNum);
        return Json(color, JsonRequestBehavior.AllowGet);
    }

the controller is called the NodeController- which is what I am referring to in url: call of ajax.

這個控制器被稱為NodeController—我在url: ajax的調用中提到它。

Basically what is happening is when i run the page, I first get an error saying it cannot set this.node.fill to a null value THEN I get the alert that an error occurred- like I said I am pretty inexperienced with javascript so I am honestly not even sure if it is calling the method in the correct order if i get an error on this.node.fill before I receive the error message from JQuery.

基本上,當我運行頁面時,我首先得到一個錯誤,說它不能設置this.node。填充到空值之后,我就會得到一個錯誤發生的警告——就像我說過的,我對javascript非常不熟悉,所以我甚至不確定如果我在this.node上發現錯誤,它是否以正確的順序調用這個方法。在收到來自JQuery的錯誤消息之前填充。

Any/all help or suggestions is greatly appreciated!!!

非常感謝您的幫助和建議!!

2 个解决方案

#1


5  

  1. If your controller class is NodeController, use only "Node" for the controller name in Url.Action:

    如果您的控制器類是NodeController,那么只在Url.Action中使用“Node”作為控制器名。

    url: '@Url.Action("CallService", "Node")',
    
  2. You cannot synchronously return a value from an asynchronous function. Either pass a callback to CallService to be called on success or use jquery promise - http://api.jquery.com/promise/

    不能從異步函數同步返回值。要么將回調傳遞給調用服務,在成功時調用它,要么使用jquery promise——http://api.jquery.com/promise/

  3. We don't know what this.node.fill is or where it is defined. Likely, this.node is not defined at the time the assignment is executed.

    我們不知道這個。node是什么。填充是或它的定義。有可能的是,這個。在執行分配時沒有定義節點。

#2


2  

You need not to write controller with controller's name

不需要用controller的名稱編寫控制器

@Url.Action("CallService", "Node")

注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2015/01/05/abe02012a55f1d12df909335b60f1a35.html



 
粤ICP备14056181号  © 2014-2021 ITdaan.com