Post Json 控制器字典
Post Json Dictionary to the controller
我的 jQuery 文件中有以下代码
var bases = {};
for (var j = 0; j < selectedVariants.length; j++) {
bases[selectedVariants[j].BId] = selectedVariants[j].CId;
}
现在我正在 bases 字典中获取一些数据
我的问题是如何通过 ajax 调用将这个基础字典传递给控制器。
我尝试了以下操作,但控制器中的碱基计数变为零
$.ajax({
url: $.url('~/TUP/Tflow'),
type: 'POST',
data: { baseDetails: JSON.stringify(bases)},
async: true,
cache: false,
});
在这里,当我在我的控制器中看到时……碱基计数为零
请帮我解决这个问题
控制器:
[HttpPost]
public JsonResult Tflow(JsonFileContentInputs basedetails)
{
//some code
}
和我的模特:
[ModelBinder(typeof(JsonModelBinder))]
[DataContract]
public class JsonFileContentInputs
{
[JsonProperty(PropertyName = "basedetails")]
[DataMember]
public Dictionary<string, string> basedetails { get; set; }
}
而不是接收 Class 对象,您应该将其作为字符串接收,然后序列化为对象,如下所示。
public JsonResult Tflow(string basedetails)
{
//some code
var model = new JavascriptSerializer().Deserialize<JsonFileContentInputs>(basedetails);
// Your code
}
试试下面的方法。作为 @EhsanSajjad mentioned, you'll need to call JSON.stringify
所有数据,而不仅仅是 bases
对象:
$.ajax({
url: '/TUP/Tflow',
type: 'POST',
data: "json=" + JSON.stringify({baseDetails: bases}), // stringify everything,
dataType: 'text',
async: true,
cache: false
});
然后在您的控制器中,我们可以使用 Json.NET.
自行反序列化数据,而不是尝试使用模型绑定
控制器:
[HttpPost]
public ActionResult Tflow(string json)
{
// deserialize
var data = JsonConvert.DeserializeObject<JsonFileContentInputs>(json);
// more code
}
型号:
// You can drop these two as we aren't using the modelbinding
// [ModelBinder(typeof(JsonModelBinder))]
// [DataContract]
public class JsonFileContentInputs
{
[JsonProperty(PropertyName = "baseDetails")]
public Dictionary<string, string> BaseDetails { get; set; }
}
不幸的是,在控制器中读取请求的原始流似乎是必要的,因为默认情况下 MVC 控制器不能很好地处理原始 JSON。 More info here.
编辑:看起来您可以将原始 JSON 传递给 MVC 控制器,您只需将 ajax 数据类型指定为 text
并确保参数名称匹配。我已经相应地更新了我的答案。
我的 jQuery 文件中有以下代码
var bases = {};
for (var j = 0; j < selectedVariants.length; j++) {
bases[selectedVariants[j].BId] = selectedVariants[j].CId;
}
现在我正在 bases 字典中获取一些数据
我的问题是如何通过 ajax 调用将这个基础字典传递给控制器。
我尝试了以下操作,但控制器中的碱基计数变为零
$.ajax({
url: $.url('~/TUP/Tflow'),
type: 'POST',
data: { baseDetails: JSON.stringify(bases)},
async: true,
cache: false,
});
在这里,当我在我的控制器中看到时……碱基计数为零
请帮我解决这个问题
控制器:
[HttpPost]
public JsonResult Tflow(JsonFileContentInputs basedetails)
{
//some code
}
和我的模特:
[ModelBinder(typeof(JsonModelBinder))]
[DataContract]
public class JsonFileContentInputs
{
[JsonProperty(PropertyName = "basedetails")]
[DataMember]
public Dictionary<string, string> basedetails { get; set; }
}
而不是接收 Class 对象,您应该将其作为字符串接收,然后序列化为对象,如下所示。
public JsonResult Tflow(string basedetails)
{
//some code
var model = new JavascriptSerializer().Deserialize<JsonFileContentInputs>(basedetails);
// Your code
}
试试下面的方法。作为 @EhsanSajjad mentioned, you'll need to call JSON.stringify
所有数据,而不仅仅是 bases
对象:
$.ajax({
url: '/TUP/Tflow',
type: 'POST',
data: "json=" + JSON.stringify({baseDetails: bases}), // stringify everything,
dataType: 'text',
async: true,
cache: false
});
然后在您的控制器中,我们可以使用 Json.NET.
自行反序列化数据,而不是尝试使用模型绑定控制器:
[HttpPost]
public ActionResult Tflow(string json)
{
// deserialize
var data = JsonConvert.DeserializeObject<JsonFileContentInputs>(json);
// more code
}
型号:
// You can drop these two as we aren't using the modelbinding
// [ModelBinder(typeof(JsonModelBinder))]
// [DataContract]
public class JsonFileContentInputs
{
[JsonProperty(PropertyName = "baseDetails")]
public Dictionary<string, string> BaseDetails { get; set; }
}
不幸的是,在控制器中读取请求的原始流似乎是必要的,因为默认情况下 MVC 控制器不能很好地处理原始 JSON。 More info here.
编辑:看起来您可以将原始 JSON 传递给 MVC 控制器,您只需将 ajax 数据类型指定为 text
并确保参数名称匹配。我已经相应地更新了我的答案。