アヤックスから送信するときにASP.net MVCコントローラヌル配列を受け取ります

flashsplat:

私は今日のように思える何のためにこれをしてきました。これはとても簡単でなければなりません。なぜこれが動作しませんか?私のURLは次のようになります。

https://example.com/photos/gallery/c15905d7-8216-4e81-ac15-2fafd10b49e8 /80515cad-070a-4d61-a7e3-f2dbb1968c9d

私が送りたいc15905d7-8216-4e81-ac15-2fafd10b49e880515cad-070a-4d61-a7e3-f2dbb1968c9d私のコントローラに。

ここで私は(20748回の試行)試した最後のものは、次のとおりです。

function setViewed() {
    var pathArray = window.location.pathname.split('/');

    $.ajax({
        type: "PUT",
        url: '/api/Customers/',
        data: { 'pathArray': pathArray },
        dataType: "json",
        traditional: true,
        success: function (response) {
            alert(response.msg);
        }
    });
}

コントローラ:

[HttpPut]
public IHttpActionResult SeenIt(List<String> pathArray)
{

    // Don't update if it's the client looking at a customer's gallery:
    if (pathArray[3] == User.Identity.GetUserId()){
        return Json(new
        {
            msg = String.Format("ClientID: {0} | CustomerID: {1}", pathArray[3], pathArray[4])
        });
    }

    var customer = db.Customers.FirstOrDefault(c => c.CustomerID == Guid.Parse(pathArray[4]));
    customer.Accessed = true;

    db.SaveChanges();
    return Json(new
    {
        msg = String.Format("ClientID: {0} | CustomerID: {1}", pathArray[3], pathArray[4])
    });
}

私の配列は常にnullです。これを行うには良い/簡単に/より効率的な方法はありますか?作品の一つ?ありがとう!

Jerdineワイズ:

オプション1

あなたのAJAX呼び出しでは、とのリストの文字列をラップする必要はありません{}単純に使用します。

data:pathArray

オプション2

プロパティがバインドされるだろうモデルとして役立つクラスを作成します。

public class ReceiveModel{
   public List<string> pathArray {get;set;}
}

次に、あなたのコントローラで

public IHttpActionResult SeenIt(ReceiveModel model)
{
   ...
}

おすすめ

転載: http://10.200.1.11:23101/article/api/json?id=398925&siteId=1