0

I'm trying to pass a collection into JSON object into MVC Action. I have tried almost all solutions provided on google,It does not pass data to action, please let me know what i'm missing:

AJAX Request

var data = [];
var questID = 100;
data.push({ QuestionTypeId: '2', QuestionId: questID, Answer: 'asdff' });
data.push({ QuestionTypeId: '2', QuestionId: questID, Answer: 'asdff' });
data.push({ QuestionTypeId: '2', QuestionId: questID, Answer: 'asdff' });
data.push({ QuestionTypeId: '2', QuestionId: questID, Answer: 'asdff' });

//var items = JSON.stringify(data);
//alert(items);
var items = JSON.stringify({ 'items': data });
alert(items);

$.ajax({
url: '/Dashboard/CreateAssessment',
data: items,
type: 'POST',
contentType: "application/json; charset=utf-8",
dataType: 'json',
traditional: true,
success: function (response) {
if (response.success) {
    swal('Password Changed', response.Message, 'success');
                        window.location.href = "/Account/Management";
                    } else {
                        $("update-password").removeAttr("disabled");
                        swal('Error', response.Message, 'error');
                    }
                },
                error: function (response) {
                    $("update-password").removeAttr("disabled");
                }
            });

Model Class

public class AssessmentQuestion
{
    public int Id { get; set; }
    public string Description { get; set; }
    public int DisplayOrder { get; set; }
    public string PreviousYearValue { get; set; }
    public QuestionType QuestionType { get; set; }
}

Controller Action Method

// POST: /Dashboard/CreateAssessment
[HttpPost]
public JsonResult CreateAssessment(List<AssessmentAnswerModel> items)
{
        var text = items;
        return Json(new { success = true });
       // return null;
        //return RedirectToAction("Thanks", "Account");
}
1
  • Change the line var items = JSON.stringify({ 'items': data }); to var items = JSON.stringify(data); and it should work. Commented Jan 10, 2017 at 14:21

2 Answers 2

1

Please check follwing code with error line identified.

var data = [];
var questID = 100;
data.push({ QuestionTypeId: '2', QuestionId: questID, Answer: 'asdff' });
data.push({ QuestionTypeId: '2', QuestionId: questID, Answer: 'asdff' });
data.push({ QuestionTypeId: '2', QuestionId: questID, Answer: 'asdff' });
data.push({ QuestionTypeId: '2', QuestionId: questID, Answer: 'asdff' });

//var items = JSON.stringify(data);
//alert(items);


var items = JSON.stringify(data); // HERE IS THE ERROR

alert(items);

$.ajax({
url: '/Dashboard/CreateAssessment',
data: items,
type: 'POST',
contentType: "application/json; charset=utf-8",
dataType: 'json',
traditional: true,
success: function (response) {
if (response.success) {
    swal('Password Changed', response.Message, 'success');
                        window.location.href = "/Account/Management";
                    } else {
                        $("update-password").removeAttr("disabled");
                        swal('Error', response.Message, 'error');
                    }
                },
                error: function (response) {
                    $("update-password").removeAttr("disabled");
                }
            });

Please recheck your AssessmentAnswerModel with case sensitive consideration

Please mark as answer if found helpful.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks - I already tried var items = JSON.stringify(data); I used this before and then commented. Ideally it should be like this only. But in controller public JsonResult CreateAssessment(List<AssessmentAnswerModel> items) it gets 0. No data
0

In controller you need to pick data as an array for example

// POST: /Dashboard/CreateAssessment
[HttpPost]
public ActionResult CreateAssessment(AssessmentAnswerModel[] items)
 {
    var text = items;
    return Json(new { success = true });

 }

I'm not sure why it happens like this but it worked like what I did.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.