Hello Stackoverflow社区,
我做了一个示例应用来说明我的问题。我使用mvc 5和Infragistics Ignite UI igGrid。这就是页面的样子。
我传递给View的模型是公司类。
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;
}
}
我的视图看起来像这样
点击“保存”按钮后,我需要保存公司和员工表中的数据。这必须在单击一次按钮时执行,因为我正在进行一些复杂的验证。我没有保存员工表的问题,但是我在保存公司属性(公司名称和公司地址)方面遇到了问题。
非常感谢。
答案 0 :(得分:3)
您可以触发自己的ajax请求,其中既可以包含igGrid事务中的数据,也可以包含输入字段中的附加数据。 网格具有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调用的success函数中,应清除网格的旧事务,因为它们已在请求期间处理过。