1

リポジトリからデータを取得し、Ajax を使用して Bootgrid を介してビューにグリッドを設定する ApiController を作成することができました。これは、Api のアクションに送信されるリクエスト データの例です。こちらのドキュメントで指定されています([POST ボディ リクエスト] タブを探します)。

current=1&rowCount=10&sort[sender]=asc&searchPhrase=&id=b0df282a-0d67-40e5-8558-c9e93b7befed

URL の例を次に示します。

http://localhost/api/SomeController?current=1&rowCount=10&sort%5BName%5D=asc&searchPhrase=&id=b0df282a-0d67-40e5-8558-c9e93b7befed

応答として返す必要があるデータを処理し、データを並べ替える (配列であるため) 2 つのヘルパー クラスを作成しました。

public class SortData
{
    public string Field { get; set; } // FIeld Name
    public string Type { get; set; } // ASC or DESC
}

public class BootgridResponseData<T> where T: class
{
    public int current { get; set; } // current page
    public int rowCount { get; set; } // rows per page
    public IEnumerable<T> rows { get; set; } // items
    public int total { get; set; } // total rows for whole query
}

したがって、私の行動は次のとおりです。

    public BootgridResponseData<SomeViewModel> Get(int current, int rowCount, List<SortData> sort, string searchPhrase, string id)
    {
       // get items and return a bootgrid response data with them...
    }

メソッドが呼び出され、常に null である sort を除いて、すべてのパラメーターが適切にデータを受け取ります。

これにはどのような種類のパラメーターを期待する必要がありますか? 私も入れようとしましたobjectが、とにかくnullになります。

4

4 に答える 4

2

もう少し学習した後、Bootgrid には、サーバーに送信されたデータを操作できるrequestHandler 設定があることがわかりました。

私は次のようにJavaScriptでそれを行いました:

var grid = $("#my-grid").bootgrid({
        ajax: true,
        rowCount: 10,
        ajaxSettings: {
            method: "POST",
            cache: true
        },
        requestHandler: function (request) {
            // Scan the original sort object from Bootgrid...
            // and populate an array of "SortData"...
            request.sortItems = [];
            if (request.sort == null)
                return request;
            for (var property in request.sort) {
                if (request.sort.hasOwnProperty(property)) {
                    request.sortItems.push({ Field: property, Type: request.sort[property] });
                }
            }
            return request;
        },
        url: "/api/FooApi"
    });

次に、API で投稿アクションを次のように作成しました。

public class FooApiController : ApiController
{

    [HttpPost]
    public BootgridResponseData<FooModel> Get(BootgridRequestData model)
    {
        // This would come from some data store, using the request params...
        // I use PagedList to make pagination easier...
        IPagedList<FooModel> itemsPaged = store.GetPagedFoo();

        // Then return the response with data...
        return new BootgridResponseData<FooModel>()
        {
            current = model.current,
            rowCount = model.rowCount,
            rows = itemsPaged,
            total = itemsPaged.TotalItemCount
        };
    }
}

BootgridResponseDataすでに私の質問に示されています。BootgridRequestData次の構造を追加しました。

public class BootgridRequestData
{
    public int current { get; set; }
    public int rowCount { get; set; }
    public string searchPhrase { get; set; }
    public IEnumerable<SortData> sortItems { get; set; }
}

次に、元のSortDataヘルパー クラスを使用することもできます。

public class SortData
{
    public string Field { get; set; } // FIeld Name
    public string Type { get; set; } // ASC or DESC
}
于 2016-08-10T06:23:27.163 に答える
1

並べ替えオプションを Web サービスに渡すのと同じ問題がありました。Dictionary オブジェクトも私の問題を解決しませんでした。それを解決するために、ブートグリッドの並べ替えオプションを通過させたい各フィールドの文字列プロパティを保持するクラスを作成しました。コードの抜粋を参照

class TestSort
{
   public string field1 { get; set; }
   public string field2 { get; set; }
   ...
}

このクラスを Web サービスのソート オプション パラメータとして使用します。bootgrid オプションによって参照されるこのクラスのすべてのフィールドは、「ASC」または「DESC」に設定されます。その他は null のままです。

このクラスに、null ではないフィールドの orderby 句を返す「orderBy」プロパティを追加しました。

于 2016-02-10T18:06:47.417 に答える
1

私もこれに苦労しました。あなたはそれを考えすぎています。jquery-bootgrid からの post 呼び出しを処理する単純なモデルを作成するのは良いことですが、post メソッドで単純なパラメーターを使用することもできます。並べ替えに関しては、キーと値のペアのように見えますが、正しくシリアル化されません。

Dictionary オブジェクトを試してみたところ、うまくいきました。

ここに私の署名があります:

[HttpPost]
public async Task<ActionResult> GetActiveDogs(int? current, int? rowCount, 
                Dictionary<string, string> sort, string searchPhrase = null)
于 2016-01-27T22:55:12.023 に答える
0

アプローチ1.列「col1、col2、col3、...」を持つテーブルがあるとします。あなたが使用することができます:

public ActionType Get(int current, int rowCount, Sort sort, string searchPhrase) {
    //sort.col1 == 'asc' (consider sorted by col1 in ascending order)
}

public class Sort
{
    public string col1 { get; set; }
    public string col2 { get; set; }
    public string col3 { get; set; }
    //... other columns
}

アプローチ 2. remove you パラメータを使用して、リクエスト データを手動で解析できます。get の代わりに post here を使用しました。

[HttpPost]
public object Post(){
    string jsonContent = Request.Content.ReadAsStringAsync().Result;
    Dictionary<string, string> keyvalues = new Dictionary<string, string>();
    string[] keyvalue_strings = jsonContent.Split('&');
    string sort_column = "";
    string sort_direction = "";
    for (var i = 0; i< keyvalue_strings.Length; i++)
    {
        var a = keyvalue_strings[i].Split('=');
        a[0] = a[0].Replace("%5B", "[").Replace("%5D", "]");
        keyvalues.Add(a[0], (a[1]));
        if (a[0].Contains("sort"))
        {
            sort_column = a[0].Replace("sort[", "").Replace("]", "");
            sort_direction = a[1];
        }
    }
    //now you have keyvalues, sort_column, sort_direction.
    //...
}
于 2019-06-06T14:00:09.267 に答える