asp.net mvc - Pass a list of objects to MVC action method using AngularJS post -
i have action method given below:
[httppost] public actionresult ask(question question) { if (modelstate.isvalid) { tempdata["newquestion"] = question; return redirecttoaction("submit"); } return view(question); }
the question
class definition given below:
public class question { public int id { get; set; } public string title { get; set; } public string body { get; set; } public string userid { get; set; } public list<tag> tags { get; set; } public int votes { get; set; } public list<answer> answers { get; set; } public int views { get; set; } public datetime creationdate { get; set; } }
the code have written call above given action method given below:
<script> function questioncontroller($scope, $http) { $scope.submit = function () { var data = $.param({ title: $scope.title, body: $scope.body, tags: [$.param({ tagname: 'mvc' }), $.param({ tagname: 'wcf' })] }); var config = { headers: { 'content-type': 'application/x-www-form-urlencoded;charset=utf-8;' } }; $http.post('ask', data, config) .success(function (data, status, headers, config) { $scope.postdataresponse = data; }) .error(function (data, status, header, config) { alert(data); }); }; } var queapp = angular.module("queapp", []); queapp.controller("quectrl", questioncontroller); </script>
the action method being called tags
member list received null
. please let me know doing wrong.
try changing content-type
value application/json
<script> function questioncontroller($scope, $http) { $scope.submit = function () { var data = { title: $scope.title, body: $scope.body, tags: [{ tagname: 'mvc' }, { tagname: 'wcf' }] }; var config = { headers: { 'content-type': 'application/json;charset=utf-8;' } }; $http.post('ask', data, config) .success(function (data, status, headers, config) { $scope.postdataresponse = data; }) .error(function (data, status, header, config) { alert(data); }); }; } var queapp = angular.module("queapp", []); queapp.controller("quectrl", questioncontroller); </script>
Comments
Post a Comment