簡體   English   中英

從返回布爾值並返回瀏覽器的方法獲取計數器值int

[英]get counter value int from method that return a bool and return to browser

我試圖弄清楚如何在foreach循環內的方法中獲得計數器的值。

我已經考慮過代表,但是我不知道該怎么做。

我的方法是刪除所有無關緊要的代碼

public static bool ReadExcelFile(string str1, int Id, out int tCount)
{
    int primaryCounter = 0;                    

    //Insert new items
    foreach (var item in excelList)
    {                
        primaryCounter++;                                
    }

    return true;
}                

然后我有一個ActionResults,用於jquery輪詢

public ActionResult InsertPolling()
{
    int currentCount = //How to get value of counter        
    string pollingMessage = $"Inserted {currentCount}  items";
    return Json(pollingMessage, JsonRequestBehavior.AllowGet);
}

我的jQuery是:

function doPollHandler(event) {
    $.post('/UploadData/InsertPolling',
        function(data) {
            alert(data); // process results here
            setTimeout(window.doPollHandler, 2000);
        });
};

$('#fileUpload').change(function () {
    doPollHandler();
});

我看過https://www.tutorialspoint.com/csharp/csharp_delegates.htm

https://docs.microsoft.com/zh-cn/dotnet/csharp/programming-guide/delegates/using-delegates

https://docs.microsoft.com/zh-cn/dotnet/csharp/programming-guide/delegates/how-to-declare-instantiate-and-use-a-delegate

但是當我的方法返回布爾值時,我需要獲取counter的值,所以我對如何執行此操作感到困惑。

希望這可以幫助您理解使用委托的回調

namespace ConsoleApplication1
{
    class Program
    {

        public delegate void Del(int message);
        static void Main(string[] args)
        {
            Del handler = DelegateMethod;
            MethodWithCallback(handler);
        }

        public static void MethodWithCallback(Del callback)
        {
            for (int i = 0; i < 50; i++)
            {
                callback(i);

            }
        }
        public static void DelegateMethod(int primaryCounter)
        {
            System.Console.WriteLine(primaryCounter);
        }
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM