Param of POST method always get null value in Asp.Net Core

Dung Do Tien Dec 04 2020 642

I have built a project by Asp.net core 3.1 and Angular JS. When I call a post method from Angular js but the parameter of the Post method in Asp.net core always Null. I code as below:

Controller

[HttpPost]
public async Task<ActionResult> GetData([FromBody]QuizSearchModel model)
{
    var response = new ResponseData();
    try
    {
        model.Keyword = !string.IsNullOrEmpty(model.Keyword) ? model.Keyword : "";
        var lstModel = new List<QuizModelOnList>();
        var lstData = await _quizBo.GetList(model.Keyword, model.LanguageId, model.Status, model.PageIndex, model.PageItem);
        if (lstData != null && lstData.Any())
        {
            lstModel = lstData.Select(x => new QuizModelOnList(x)).ToList();
        }

        response.Success = true;
        response.Data = lstModel;
        return Json(response);

    }
    catch (Exception ex)
    {
        response.Success = false;
        response.Message = ex.Message;
        return Json(response);
    }
}

Angular JS

$scope.modelSearch = {
    'Keyword': "",
    'LanguageId': '0',
    'Status': 0,
    'PageIndex': 1,
    'PageItem': 10
};


$scope.GetData = function () {

    $scope.modelSearch.PageIndex = $scope.page.currentPage;
    $scope.modelSearch.PageItem = $scope.page.itemsPerPage;

    var url = "/QuizDev/GetData";
    var model = {
        Keyword: $scope.modelSearch.Keyword,
        LanguageId: $scope.modelSearch.LanguageId,
        Status: $scope.modelSearch.Status,
        PageIndex: $scope.modelSearch.PageIndex,
        PageItem: $scope.modelSearch.PageItem
    };

    $http.post(url, model).then(function (response) {
        if (response !== null) {
            if (response.data.success) {
                $scope.ListDataQuiz = response.data.data;
                $scope.page.totalItems = response.data.totalRow;
            } else {
                alert(response.data.message);
            }
        }
    });
};

QuizSearchModel model: This param of GetData() method always null. 

Please suggest to me if you know any solution.

Have 2 answer(s) found.
  • M

    Marry Christ Dec 04 2020

    I see a problem ion your code. In Angular js file, you have to change :

     $scope.modelSearch = {
        .................  
        'LanguageId': '0',
         .................
    }; 

    To

     $scope.modelSearch = {
        .................  
        'LanguageId': 0,
         .................
    }; 

    If property LanguageId in param of action is integer type, in angular it also has to number data type.

  • M

    Manish Kumar Dec 04 2020

    I got the same issue before. I add the line code below to ConfigureServices() method of Startup.cs. It worked for me:

    services.AddControllers(x => x.AllowEmptyInputInBodyModelBinding = true); 

    AllowEmptyInputInBodyModelBinding = true : This option help gets or sets the flag which decides whether body model binding (for example, on an action method parameter with Microsoft.AspNetCore.Mvc.FromBodyAttribute) should treat empty input as valid. false by default.

    I hope this solution will resolve the issue for you.

Leave An Answer
* NOTE: You need Login before leave an answer

* Type maximum 2000 characters.

* All comments have to wait approved before display.

* Please polite comment and respect questions and answers of others.

Popular Tips

X Close