2016-09-23 16 views
1

モデルオブジェクトのリストを使用してテーブルを作成しました。 その後、その行のボタンがクリックされると、行データがAjaxポストに渡されます。null json文字列をmvcコントローラに渡す方法を解決するには?

Ajaxポスト内で.stringifyが呼び出され、その行にデータが渡されます。 私は彼らが装着されていることを見ることができます開発ツールにワイヤ上で渡されるデータの値をチェックします。

["66", "[email protected]", "2009", "test",…] 
0 
: 
"66" 
1 
: 
"[email protected]" 
2 
: 
"2009" 
3 
: 
"test" 

をしかし、私は、クライアント側から呼び出されたコントローラのPOSTアクションにステップするとき。予想されるJSON文字列はnull/emptyです。私は考えているかもしれないが、おそらくこれはbeacauseですstringify配列の各値に関連付けるプロパティ名がありません。

質問:

どのようにMVCコントローラに渡されたヌルJSON文字列を解決することができますか?

public class Status 
{ 

     [Key] 
     public int ID { get; set; } 


     public string Contact_Email { get; set; } 

     public string RID { get; set; } 

     public string Name { get; set; } 



    } 

表およびAJAXポスト方法:

 <table id="statusTable" class="table table-hover table-bordered results"> 
      <thead> 
       <tr> 
        <th>ID</th> 
        <th>Email</th> 
        <th>RID</th> 
        <th>Name</th> 
        <th>Update Record</th> 


       </tr> 
      </thead> 
      <tbody> 
      @foreach (var row in Model.ReleaseStatus) 
      { 
        <tr> 
         <td>@Html.Raw(row.ID)</td> 
         <td>@Html.Raw(row.Contact_Email_Name)</td> 
         <td>@row.RID</td> 
         <td>@row.Name</td> 
         <td><button type="submit" class="btn btn-success">Update</button></td> 
        </tr> 
       } 
      </tbody> 

     </table> 



$(".btn-success").click(function() { 
      var $td = $(this).closest('tr').children("td"), 
      len = $td.length; 
      var tableData = $td.map(function (i) { 
       if (i < len - 1) 
        return $(this).text(); 
      }).get(); 

      console.log(tableData); 

      //Post JSON data to controller 
      $.ajax({ 
       type: "POST", 
       url: 'updateStatus', 
       data: JSON.stringify(tableData), 
       contentType: "application/json; charset=utf-8", 
       success: function (response) { 
        console.log("post success"); 
       }, 
       error: function (request) { 
        console.log("post error" + request.error); 
       } 

      }); 


     }); 

とMVCコントローラでの最終的POSTメソッド:

モデル - ここ

は、以下の実施の骨子であります
[HttpPost] 
    public ActionResult updateStatus(stirng jsonString) 
    { 
     //deserialise the json to a Status object here 
    } 
+2

を試してみてくださいモデルの代わりに 'string'にバインドします。 –

+0

mvcアクションでonjectを指定しようとしましたが、あなたのメソッドは 'public ActionResult updateStatus(Status model)'とデータ 'var data = {ID:66、Contact_Email: '[email protected]' updateStatus(Status vm)のように、オブジェクトはポスト中にヌルです。私の主な問題は、データがjqueryで渡されるということは、値に名前が付けられていないということですね。 –

+0

はい、私は前のコメントと同じように生成してオブジェクトを必要とし、ajaxでは 'contentType:" application/json; charset = utf-8 "、' data:data、 'を使用する理由はありますが、 –

答えて

6

json文字列ではなく、実際のオブジェクトを受け入れるためにコントローラを作成する必要があります。これにより

iは

[HttpPost] 
public ActionResult updateStatus(stirng jsonString) 
{ 
    //deserialise the json to a Status object here 
} 

はあなたのviewmodelを複製JSONオブジェクトを渡す必要があります StatusにあなたのJSONをバインドするためのモデルバインダーためには

[HttpPost] 
public ActionResult updateStatus(Status vm) 
{ 
    //no need to deserialize - was already done by the model binder 
} 

であるべき意味します。

擬似コードで
{ 
ID:yourId, 
Contact_Email:yourContactEmail, 
RID:YourRID, 
Name:yourName 
} 

、あなたは可能性:

var statusData = { 
    ID:tableData[0], 
    Contact_Email:tableData[1], 
    RID:tableData[2], 
    Name:tableData[3] 
}; 

その後、あなたのAJAX呼び出しで、

data: JSON.stringify(statusData), 
+0

しかし、もっと良い説明があれば、より良いものになるでしょう – Fabjan

+1

OPでも配列をポストするのではなく、オブジェクトがビューに正しく生成されない限り、これは何もしません。 –

+1

json文字列の前にStatusオブジェクトを渡してみました。アクションメソッドに到達すると、オブジェクトはnullになります。私は、それは私のデータが名前を指定していないためだと思う? –

-2

はなぜあなたがしようとしている身体の属性から

[HttpPost] 
public ActionResult updateStatus([FromBody]Status status) 
{ 
    //add the serialize attribute on your model 
} 
関連する問題