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!

3 Answers 3

1

Why not just return Json(YOR_DATA_YEAR) and handle it in success handler of your JavaScript ajax function.

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); 
 }
Sign up to request clarification or add additional context in comments.

2 Comments

but in this way, only in the end of the calling, I know the number of succeded ads. it's hard me to explain but I want that the alert message will be updated: "1/5 ads were updated" and when the second ad was updated, alert message: "2/5 ads were updated", etc.
@AlonShmiel: you are talking about PUSH notification from server to the client. Server says: I'm done this one=> signal, done another => signal. For this have a look please on: signalr.net. WebSocket, persitant communicaton between server and client. OR you can make different AJAX call for every Ids, call next on success of previous, creating artificially a chain of calls to the server.
1

You can return a JsonResult from your controller like so:

[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);
 }

You can then do an alert in your Javascript to inform the user.

 $.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) {
     } });

2 Comments

but in this way, only in the end of the calling, I know the number of succeded ads. it's hard me to explain but I want that the alert message will be updated: "1/5 ads were updated" and when the second ad was updated, alert message: "2/5 ads were updated", etc.
Oh so you want real-time pushed notifications? Firstly you cannot use an alert for that since you cannot update an alert box's text. As for the notification you can either call the service multiply times in a loop for each Id or use something like SignalR asp.net/signalr
0

is there an option that the function of c# will update online a variable in my js file

No, your ASP.NET service knows absolutely nothing about the client your only option is to pass the information it needs back as part of the post e.g.

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

Then in your success callback

success: function (serverResult) {
    alert(serverResult+ '/' + adsIds.length + ' ads were updated');
}

3 Comments

but in this way, only in the end of the calling, I know the number of succeded ads. it's hard me to explain but I want that the alert message will be updated: "1/5 ads were updated" and when the second ad was updated, alert message: "2/5 ads were updated", etc.
@AlonShmiel ah, I understand. You would need to make multiple calls to the server in that case and update a counter on the client-side.
ok, I wanted to hear that there is a way but thank you again!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.