2017-07-21 7 views
0

私はJSON結果を返すはずの単純なコントローラメソッドを持っていますが、nullを返します。 jQuery $.ajaxを使って呼び出す必要があります。私はPostmanを使ってそれをテストし、nullを返します。私は何が間違っているのか分かりません。ASP.NETコアポストアクションがnullを返す

私は次のようなJSONデータを使用して、ポストマンでそれをテストし、それはnullを返します:私は、次のJavaScriptを使用して、それています私のアプリケーションで

// Post: Contact 
[HttpPost, ActionName("GetContact")] 
public JsonResult GetContactPost([FromBody] long id) 
{ 
    var contact = _context.Contact.SingleOrDefault(m => m.ContactId == id); 
    return Json(contact); 
} 

:ここ

{ "id": 2 } 

は、コントローラのメソッドであります

function GetContact(id) { 
    $.ajax({ 
     type: "POST", 
     url: "@Url.Action("GetContact","Contacts")", 
     data: { id: id }, 
     contentType: "application/json; charset=utf-8", 
     dataType: "json", 
     success: function (result) { 
      alert("id: " + id + " result: " + result); 
     }, 
     failure: function (response) { 
      alert(response.d); 
     } 
    }); 
} 
+1

'data:JSON.stringify({id:id})'を試してください。 – Win

+0

[Asp.Net Core:json post int型の可能な複製は常に0です](https://stackoverflow.com/questions/45174143/asp-net-core-json-post-int-type-is-always-0 ) – Set

答えて

0

私はそれを理解しました。パラメータはモデルまたはビューモデルオブジェクトでなければなりません。

// Post: Contact 
    [HttpPost, ActionName("GetContact")] 
    public JsonResult GetContactPost([FromBody] ContactVM findContact) 
    { 
     var contact = _context.Contact.SingleOrDefault(m => m.ContactId == findContact.Id); 
     return Json(contact); 
    }