Steve Scott
Steve Scott

Reputation: 1522

Receiving a C# Enum in JavaScript and passing it to a model via Ajax

In my code, I need to receive a string, convert it into a C# enum, and pass the enum to my model. The code is within a for-loop (not shown).

 var thisDwm = document.getElementById("dwm_" + i).value;       
            var urlDwm = "/KnowledgeTransfer/GetDailyWeeklyMonthlyEnum";
            var response
            $.ajax({
                type: 'GET',
                url: urlDwm,
                data: { 'caseFromJS': thisDwm },
                contentType: 'application/json',
                success: function (thisResponse) {
                    response = thisResponse;
                }
            })
            model.MainResponsibilities[i].DailyWeeklyMonthly = response;

Currently the ajax hits my C# controller and returns the correct enum. But when I look at the JavaScript breakpoints, "response" remains undefined and is not set. How do I set the response to a C# enum object that I can pass to my model? Is there some parsing that needs to happen? It seems like I am not receiving anything from the controller at all.

One thing that doesn't help is that type, data, contentType, success, and thisResponse all show as "having a reference error is undefined" in Chrome. Yet the Ajax still works, at least in other places, even though these other methods also show this error.

Thank you for your assistance.

Edit: below is my controller method:

public ObjectModel.Enums.DailyWeeklyMonthly GetDailyWeeklyMonthlyEnum(string caseFromJS)
        {
            switch (caseFromJS){

                case "Daily":
                    return ObjectModel.Enums.DailyWeeklyMonthly.Daily;
                case "Weekly":
                   return ObjectModel.Enums.DailyWeeklyMonthly.Weekly;
                case "Monthly":
                   return ObjectModel.Enums.DailyWeeklyMonthly.Monthly;
                case "Yearly":
                    return ObjectModel.Enums.DailyWeeklyMonthly.Yearly;
                default:
                    throw new Exception("problem with dailyWeeklyMonthly in KnowledgeTransferController");

Upvotes: 0

Views: 1298

Answers (1)

Shyju
Shyju

Reputation: 218852

With the code you shared, you will get response is not defined error if you are trying to use the response variable before it is defined. In the code snippet you shared, you are initializing it only inside the success callback method of your ajax call.

Remember, ajax is asynchronous. When JavaScript framework executes this line model.MainResponsibilities[i].DailyWeeklyMonthly = response;, which is outside the success callback scope, The ajax call might be still executing/waiting for the response from the server, this means nothing has been set to response, which means the variable response is not initialized!

Access the response for your ajax call only inside the success or done callback.

$.ajax({
         type: 'GET',
         url: urlDwm,
         data: { 'caseFromJS': thisDwm },
         contentType: 'application/json',
         success: function (thisResponse) {
              // safe to use thisResponse in this callback method scope
              console.log(thisResponse);

              // Assuming model.MainResponsibilities[i] exist
              model.MainResponsibilities[i].DailyWeeklyMonthly = thisResponse;
         }
})

Upvotes: 2

Related Questions