8

I need to call a web api, this web api method is accepting two parameter one is enum type and another is int. I need to call this api using Ajax Jquery. How to pass enum paramter.

API Code

[HttpGet]  
public HttpResponseMessage GetActivitiesByType(UnifiedActivityType type, int pageNumber)
{     
    var activities = _activityService.GetAllByType(type, pageNumber);

    return Request.CreateResponse(HttpStatusCode.OK, activities);
}

My Ajax Call Code:

var jsonData = { 'type': 'Recommendation', pageNumber: 0 };

$.ajax({
    type: 'get',
    dataType: 'json',
    crossDomain: true,
    url: CharismaSystem.globaldata.RemoteURL_ActivityGetAllByType,
    contentType: "application/json; charset=utf-8",
    data: jsonData,
    headers: { "authorization": token },
    error: function (xhr) {
        alert(xhr.responseText);
    },
    success: function (data) {
        alert('scuess');
    }
});

Any Suggestions..

0

2 Answers 2

9

An enum is treated as a 0-based integer, so change:

var jsonData = { 'type': 'Recommendation', pageNumber: 0 };

To this:

var jsonData = { 'type': 0, pageNumber: 0 };

The client needs to know the index numbers. You could hard code them, or send them down in the page using @Html.Raw(...), or make them available in another ajax call.

Sign up to request clarification or add additional context in comments.

2 Comments

It didn't worked for me it gave me error "500 Critical Exception".
In my case, I had to set the parameter as an integer then cast it in the controller public ActionResult Index(int membershipType) { MembershipType memberType = (MembershipType)membershipType; }
3

You can not pass enum from client side. Either pass string or int and on the server side cast that value to enum. Something like,

 MessagingProperties result;
 string none = "None";
 result = (MessagingProperties)Enum.Parse(typeof(MessagingProperties), none);

Or you can do something similar to this post

Comments

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.