MVC不接受参数作为枚举

时间:2019-01-30 07:16:17

标签: ajax asp.net-mvc-4 enums

我正在尝试通过AJAX调用将js中的参数传递给我的MVC控制器,但它将其解释为null。他们有办法解决这个问题吗?

AJAX通话

service.GetYearsByType = function (docType) {
    var response = $http({
        method: 'GET',
        url: '/Budget/GetYearsByType/' + docType
    });
    return response;
}

MVC

public ActionResult GetYearsByType(DocType? docType)
{
    .....
}

我尝试将http调用更改为POST,但仍然无法正常工作。我将docType?更改为int?,并且mvc能够识别它,因此问题必定是enum变量DocType

1 个答案:

答案 0 :(得分:0)

我不这么认为,参数枚举类型引起了问题。您可以尝试以下一种方法:

service.GetYearsByType = function (docType) {
    $.ajax({

    method: 'POST',
    dataType: 'json',
    url: '/Budget/GetYearsByType/',
    contentType: "application/json; charset=utf-8",
    data: { 'docType': docType},
    success: function (data) {
        alert('scuess');
    },
    error: function (xhr) {
        alert(xhr.responseText);
    },
    });
}
相关问题