pass enum parameter from Ajax Jquery to MVC web api
13,090
Solution 1
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.
Solution 2
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
Related videos on Youtube
Author by
user3215002
Updated on September 14, 2022Comments
-
user3215002 over 1 year
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..
-
user3215002 over 10 yearsIt didn't worked for me it gave me error "500 Critical Exception".
-
Levi Fuller almost 7 yearsIn 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; }