0

I have a function in c# that gets a budget and Id-s of ads and updates all the budgets of these ads to be with budget value of budget:

[HttpPost]
public ActionResult UpdateAdsBudgets(string budget, string[] Ids)
{
    ServerResult serverResult = null;
    try
    {
        int numOfSuccess = 0;
        for (int i = 0; i < Ids.Length; i++)
        {
            serverResult = UpdateAdBudget(Ids[i]);
            if (serverResult.ServerResultState == ServerResultState.SUCCESS)
            {
                numOfSuccess++;
            }
         }
     }
 }

I called this function in my js file:

$.ajax({
    dataType: 'json',
    type: "POST",
    traditional: true,
    url: "/AdsController/UpdateAdsBudgets",
    data: { budget: budget, Ids: adsIds },
    success: function (serverResult) {

    },
    error: function (exception) {
    }
});

Is there an option to display a message with an info of the called function? I mean to: 2/5 ads were updated, where 2 is numOfSuccess (the variable of my c# file) and 5 is: adsIds.length.

I know I can do a lot of ajax calling (for each ad) and then count it in the succes, but is there an option that the function of c# will update online a variable in my js file? (assuming the variable: "numberOfSuccededAds").

any help appreciated!

4

3 回答 3

1

您可以像这样从控制器返回JsonResult

[HttpPost]
public ActionResult UpdateAdsBudgets(string budget, string[] Ids)
{
    ServerResult serverResult = null;
    try
    {
        int numOfSuccess = 0;
        for (int i = 0; i < Ids.Length; i++)
        {
            serverResult = UpdateAdBudget(Ids[i]);
            if (serverResult.ServerResultState == ServerResultState.SUCCESS)
            {
                numOfSuccess++;
            }
         }
     }
    return Json(numOfSuccess);
 }

然后,您可以在 Javascript 中发出警报以通知用户。

 $.ajax({
     dataType: 'json',
     type: "POST",
     traditional: true,
     url: "/AdsController/UpdateAdsBudgets",
     data: { budget: budget, Ids: adsIds },
     success: function (serverResult) {
        alert(serverResult + "/" + adsIds.length + " ads were updated");
     },
        error: function (exception) {
     } });
于 2013-11-13T15:04:53.297 回答
1

为什么不只是在你的ajax 函数的成功处理程序中return Json(YOR_DATA_YEAR)处理它。JavaScript

public ActionResult UpdateAdsBudgets(string budget, string[] Ids)
{
    ServerResult serverResult = null;
    try
    {
        int numOfSuccess = 0;
        for (int i = 0; i < Ids.Length; i++)
        {
            serverResult = UpdateAdBudget(Ids[i]);
            if (serverResult.ServerResultState == ServerResultState.SUCCESS)
            {
                numOfSuccess++;
            }
         }
     }

     return Json(YOUR_DATA); 
 }
于 2013-11-13T15:00:14.413 回答
0

是否有一个选项,c# 的函数将在线更新我的 js 文件中的变量

不,您的 ASP.NET 服务对客户端一无所知,您唯一的选择是将它需要的信息作为帖子的一部分传回,例如

[HttpPost]
public ActionResult UpdateAdsBudgets(string budget, string[] Ids)
{
    ...
    return Json(numOfSuccess);
}

然后在你的success回调中

success: function (serverResult) {
    alert(serverResult+ '/' + adsIds.length + ' ads were updated');
}
于 2013-11-13T15:03:21.233 回答