有办法将JSON数据发布到MVC5操作吗?而不是WebAPI!
问题是:我的操作会被调用,但是传入模型的所有属性都是空的。
型号:
using Newtonsoft.Json;
[JsonObject()]
[Serializable()]
public class DemoModel
{
[JsonProperty()]
public string a { get; set; }
[JsonProperty()]
public string b { get; set; }
}行动是:
[HttpPost()]
public ActionResult demoaction(DemoModel model)
{
//model.a here is null, should be "AAA"
//model.b here is null, should be "BBB"
}我一直在用Fiddler来模拟请求。我的帖子请求如下:
POST http://localhost:9000/demoaction HTTP/1.1
Host: localhost:9000
Connection: keep-alive
Accept: */*
Content-Type: application/json
Content-Length: 60身体看起来是这样的:
{"a":"AAA","b":"BBB"}所以,再问一次:我如何将JSON发布到一个操作中?在我的例子中,模型的所有属性都是空的。
发布于 2017-06-01 21:34:40
我只是使用这些代码,它在MVC5操作上工作得很好,但是要确保在POST请求头中使用的是Content-Type : application/json,而不知道它不能正确工作,而且模型仍然是空的。
public class DemoModel
{
public string a { get; set; }
public string b { get; set; }
}
[HttpPost]
public ActionResult Index(DemoModel model)
{
return View();
}https://stackoverflow.com/questions/44203327
复制相似问题