我想通过jQuery ajax发布一些数据到Asp.Net MVC控制器。我有我一起工作的后续类:传递json到mvc控制器
public class InnerStyle
{
[JsonProperty("color")]
public string HeaderColor { get; set; }
[JsonProperty("font-size")]
public string HeaderFontSize { get; set; }
[JsonProperty("font-family")]
public string HeaderFontFamily { get; set; }
}
POST方法是这样的:
public JsonResult UpdateRecord(InnerStyle innerStyle)
{
//Do some validation
return Json("OK");
}
我的jQuery的样子:
$('#font-size-ddl').change(function() {
var value = $(this).val();
headerObj["font-size"] = value;
console.log(JSON.stringify({ innerStyle: headerObj }));
$.ajax({
type: "POST",
url: "@Url.Action("UpdateRecord", "Document")",
data: JSON.stringify({ innerStyle: headerObj}),
contentType: "application/json; charset=utf-8",
success: function (data) {
console.log(data);
}
});
});
在中的console.log以上更改事件产生以下JSON字符串:
{"innerStyle":{"text-align":"","font-size":"20px","color":""}}
现在我遇到的问题是,如果我在我的UpdateRecord
操作中设置了一个断点,并看看innerStyle
对象中的内容是否为null。有人能告诉我我要去哪里吗?
似乎[JsonProperty]来自Newtonsoft.Json。 Asp.net控制器不支持它 –
@DanilEroshenko那么我还有什么其他选择来实现我想实现的目标? – Code
你可以看看Model Binder ..简而言之,数据应该在Request中可用。 – user2580925