0

I am trying to pass a javascript object that contains

question(string), multianswers(answer(string),correct(bit)).

This is the value of multianswers -

[{'correct':true,'answer':'A1'}, {'correct':true,'answer':'A2'}];

What type should be the parameter that I am passing to the controller.

function setMultiQuestion(question, responses) {

    var responsesList = angular.toJson(responses);

    function questionAnswerObj(question, answers)
    {
        this.question = question;
        this.answers = answers;
    }

    qaObject = new questionAnswerObj(question, responsesList);

    $http.post(baseUrl + "Admin/insertMultiAnswers", { data: qaObject })
  .success(function (data, status, headers, config) {

      $mdDialog.hide();
      $mdToast.show(
      $mdToast.simple()
     .textContent('New question was added.')
     .position('top')
     .hideDelay(5000)
     .theme('success-toast')
     );
  })
  .error(function (data, status, header, config) {
  });
}

And this is my controller.

 [HttpPost]
    public ActionResult insertMultiAnswers(object data)
    {
        try
        {
            Console.Write(data);
          //  data.setMultiAnswer();
            Response.StatusCode = 200;
            return Content("Sucess");
        }
        catch (Exception ex)
        {
            Response.StatusCode = 500;
            return Content("Fail");
        }
    }
janw
  • 6,672
  • 6
  • 26
  • 45

1 Answers1

0

What type should be the parameter that I am passing to the controller.

string[] data

This is due to the [{'correct':true,'answer':'A1'}, {'correct':true,'answer':'A2'}];. Brackets mean array. {} mean object. So your array consists out of objects containing values.

To further manipulate the array in the controller, I would suggest looking at these examples. http://sarangasl.blogspot.nl/2014/01/pass-complex-javascript-object-to-mvc.html

You could for example create a class containing a bool correct and string answer, and cast the objects in the array in the controller to this class. This makes it easy to use them later on. Example of a plugin which simplifies this process is Deserializing JSON to .NET object using Newtonsoft (or LINQ to JSON maybe?)

EDIT:

Not sure what your function does but it does not return anything. Try changing it to:

function questionAnswerObj(question, answers)
{
    var object = {
                   question: question,
                   answers: answers
                 }
    return object;
}

var object = {} creates a new object. It makes sense that the parameter in your controller is null due to the function not returning anything, which is the same as null. If you apply these changes, you will have to replace the parameter type in the controller to just string since we are not sending an array but an object.

Community
  • 1
  • 1
Olaf
  • 1,038
  • 8
  • 20
  • Thank you. But the object contains - question(string) + json array(correctAns( bit), answer(string)) –  Nov 17 '16 at 11:30
  • I followed your suggestion and changed my code like: public ActionResult insertMultiAnswers(string[] data) . But still null. I didn't change anything in the javascript controller. –  Nov 17 '16 at 11:50
  • function questionAnswerObj(question, answers) { this.question = question; this.answers = answers; } – Olaf Nov 18 '16 at 08:30
  • By the way, http://json2csharp.com/ is a great tool to quickly create c# classes out of json object, to use in serializers like newtonsoft. – Olaf Nov 18 '16 at 08:37