2017-05-26 14 views
0

JSONデータをMVC5アクションにPOSTする方法はありますか? WebAPIではありません!JSONからASP.NET MVC5へのPOST? (Web APIではない)

問題は:私のアクションは呼び出されますが、着信モデルのすべてのプロパティはnullです。

モデル:

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" 
} 

。私のPOSTリクエストは、次のようになります

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を投稿することができますか?私の場合、モデルのすべてのプロパティはnullです。

+0

FromBody属性があるだろうことなしに、あなたのPOSTリクエストヘッダ内のContent-Type : application/jsonを使用していることを確認し非WebAPIでは利用できません:/ – Dima

+1

あなたは正しいです。謝罪いたします – Nkosi

答えて

0

私はこれらのコードを使用して、それはMVC5アクション にうまく働いていますが、それは正しく動作しませんし、モデルはまだヌル

public class DemoModel 
    { 

     public string a { get; set; } 


     public string b { get; set; } 
    } 

[HttpPost] 
     public ActionResult Index(DemoModel model) 
     { 

      return View(); 
     } 
関連する問題