私は多くの同様の質問を見て、考えられるすべての答えを試しましたが、私の問題はまだ同じです. 基本的に、次の ajax 呼び出しがあります。
var obj = {
Duration: 12000,
Errors: 2
};
var post = $.ajax({
url: APIUrl,
data: JSON.stringify(obj),
type: "POST",
cache: false,
dataType: 'json',
contentType: 'application/json, charset=utf-8'
});
これが私のビューモデルです。
public class TargetBasedViewModel
{
public int Duration{ get; set; }
public int Errors{ get; set; }
}
そして、これが私のコントローラーです。
[HttpPost]
public HttpResponseMessage Post(TargetBasedViewModel test)
{
return new HttpResponseMessage(HttpStatusCode.OK);
}
問題は、パラメータが null のままになることです。Fiddler を使用してリクエストを調べたところ、問題ないように見えます。すべての属性がそこにあり、JSON オブジェクトを調べて、値が実際にサーバーに送信されていることを確認することもできます。多くの記事を読み、[ModelBinder] と [FromBody] をパラメーターに追加しようとしましたが、まだ運がありません。これを Application_Start() に追加しようとしました
GlobalConfiguration.Configuration.Formatters.JsonFormatter.SerializerSettings = new JsonSerializerSettings();
私が間違っているかもしれないアイデアはありますか?