无法从ajax到控制器获取数组

问题描述 投票:0回答:1

我有一个对象数组,我使用ajax从视图传递到控制器。数组数据是:enter image description here

我的ActionMethod是:

public JsonResult AddQuestionsToStep(long stepId, string questionText, string questionType, string correctAnswer = "", List<QuestionOption> choices = null)

我正在接收其他变量数据并计算选择数组,但选择数组中的数据未映射。即,在客户端具有值的OptionName在服务器端为空。我做错了什么?

jquery ajax asp.net-mvc knockout.js
1个回答
1
投票

由于你的choices是一个对象的javascript数组,序列化它并在你的后端解析它:

choices: JSON.stringify(choices)

在你的后端只需解析json:

List<QuestionOption> choices = (List<QuestionOption>) JsonConvert.DeserializeObject(choicesJson, typeof(List<QuestionOption>));
© www.soinside.com 2019 - 2024. All rights reserved.