Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pass enum parameter from Ajax Jquery to MVC web api

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..

like image 911
user3215002 Avatar asked Jan 20 '14 12:01

user3215002


2 Answers

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.

like image 50
Greg Ennis Avatar answered Nov 02 '22 04:11

Greg Ennis


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

like image 34
gmail user Avatar answered Nov 02 '22 04:11

gmail user