1

Stackoverflow コミュニティの皆様、こんにちは。

私の問題を説明するために、小さなサンプル アプリケーションを作成しました。mvc 5 と Infragistics Ignite UI igGrid を使用しています。ページはこんな感じです。

マイページ

ビューに渡すモデルは Company クラスです。

public class Company
{
     public int ID { get; set; }
     public string CompanyName { get; set; }
     public string Address { get; set; }
     public IQueryable<Employee> EmployeeList { get; set; }
}

And the Employee Class: 

public class Employee
{
      public int ID { get; set; }
      public string FirstName { get; set; }
      public string LastName { get; set; }
}

これが私のコントローラーの外観です

    // GET: Company
    public ActionResult Index()
    {
        Company myCompany = new Company { ID = 1, CompanyName = "Cool Company", Address = "USA" };
        Employee employee1 = new Employee { ID = 10, FirstName = "Christiano", LastName = "Ronaldo" };
        Employee employee2 = new Employee { ID = 11, FirstName = "Sebastian", LastName = "Schweinsteiger" };

        List<Employee> employeeList = new List<Employee>();
        employeeList.Add(employee1);
        employeeList.Add(employee2);
        myCompany.EmployeeList = employeeList.AsQueryable();
        return View("Index", myCompany);

    }

    [HttpPost]
    public ActionResult SaveData()
    {

        GridModel gridModel = new GridModel();
        List<Transaction<Employee>> transactions = gridModel.LoadTransactions<Employee>(HttpContext.Request.Form["ig_transactions"]);
        // here im getting the changes of the employee table. This works fine. But how do I get the changes of my company?

        foreach (Transaction<Employee> t in transactions)
        {
            //do something.. 
        }

        JsonResult result = new JsonResult();
        Dictionary<string, bool> response = new Dictionary<string, bool>();
        response.Add("Success", true);
        result.Data = response;
        return result;
    }
}

私のビューは次のようになります

ここに画像の説明を入力

ここに画像の説明を入力

「保存」ボタンをクリックした後、会社と従業員テーブルからデータを保存する必要があります。複雑な検証を行っているため、これはボタンを 1 回クリックするだけで実行する必要があります。従業員テーブルの保存に問題はありませんが、会社の属性 (会社名と会社の住所) の保存に問題があります。

どうもありがとうございました。

4

1 に答える 1

3

igGrid のトランザクションからのデータと入力フィールドからの追加データの両方を含めることができる独自の ajax 要求をトリガーできます。グリッドには、サーバーに送信する準備が整った文字列化されたデータを直接返すtransactionsAsStringメソッドがあります。コードがどのように見えるかの例を次に示します。

$("#saveChanges").on("igbuttonclick", function () { 
            var companyName = $("#CompanyName").val();
            var address = $("#Address").val();

            var companyData = JSON.stringify({ "CompanyName": companyName, "Address": address });

            var gridTransactions = grid.igGrid("transactionsAsString");

            var opts = {
                type: "POST",
                url: "@Url.Action("SaveData")",
                data: { "ig_transactions": gridTransactions, "companyData": companyData },
                success: function (data, textStatus, jqXHR) {
                    //clear transaction log on success
                    grid.igGrid("allTransactions").clear();
                },
                error: function (jqXHR, textStatus, errorThrown) {
                }
            };
            $.ajax(opts);
});

コントローラーで同じアクションにヒットします。グリッドからトランザクションをデシリアライズするコードはまったく同じです。会社のデータも逆シリアル化するには、JavaScriptSerializer を使用するか、渡されたデータを逆シリアル化する他の手段を使用できます。次に例を示します。

JavaScriptSerializer serializer = new JavaScriptSerializer();
Company companyData = serializer.Deserialize<Company>(HttpContext.Request.Form["companyData"]);

ajax 呼び出しの成功関数では、グリッドの古いトランザクションは、要求中に既に処理されているため、クリアする必要があることに注意してください。

于 2016-07-21T13:47:52.530 に答える