我陷入了困境。我使用ajax将数据传递给 Controller , Controller 有一个方法将数据传递给DAL,DAL返回一个int,即新创建的插入记录的ID。我的问题是如何获取返回的 ID 并将其放入 LocalStorage 中?
这是要提交的新 vendor
function NewVendorToSubmit() {
var isItChecked;
if ($("#sTaxEnabled").prop("checked")) {
isItChecked = "1";
}
else isItChecked = "0";
var Vendor = {
VendorName: $("#txtVendorName").val(),
Address1: $("#txtVendorAddress1").val(),
Address2: $("#txtVendorAddress2").val(),
City: $("#txtVendorCity").val(),
State: $("#acVendorState").val(),
Zip: $("#txtVendorZip").val(),
VendorPhone: $("#txtVendorPhone").val(),
VendorFax: $("#txtVendorFax").val(),
Email: $("#txtEmail1").val(),
Email2: $("#txtEmail2").val(),
TaxEnabled: isItChecked
};
return Vendor;
}
这是我的 jquery 函数,它在按钮保存事件上被调用
function SubmitNewVendor() {
var newVendor = NewVendorToSubmit();
$.ajax({
type: "POST",
url: URLParam.AddNewVendor,
dataType: "json",
contentType: "application/json; charset=utf-8",
data: JSON.stringify(newVendor),
success: function (data, textStatus, jqXHR) { },
complete: function (e) {
ClearVendorFields();
GetVendorGridData();
//CloseTheWindow();
}
})
}
这是它正在调用的 Controller 方法
[HttpPost]
public int AddVendor(NewVendorToAdd nvta)
{
DAL = new UsherDAL();
int addedVendorID = DAL.AddNewVendor(nvta);
return addedVendorID;
}
这是从 DAL 调用的方法
public int AddNewVendor(NewVendorToAdd nvta)
{
Vendor vendor = new Vendor();
Address address = new Address();
VendorAddressXREF vendoraddressxref = new VendorAddressXREF();
vendor.VendorName = nvta.VendorName;
int newVendorID = InsertVendor(vendor);
address.Line1 = nvta.Address1;
address.Line2 = nvta.Address2;
address.City = nvta.City;
address.ZipCode = nvta.Zip;
address.StateID = nvta.State;
int newAddressID = InsertAddress(address);
vendoraddressxref.VendorID = newVendorID;
vendoraddressxref.AddressID = newAddressID;
int newVendorAddressXREFID = InsertVendorAddressXREF(vendoraddressxref);
return newVendorID;
}
那么我该如何从 AddNewVendor 获取返回的 int 并将其放入 LocalStorage,而无需再次往返数据库?
最佳答案
似乎 vendor 的 ID 作为对 POST
请求的响应而返回,要将其存储在本地存储中,您可以执行以下操作:
$.ajax({
...,
success: function (data, textStatus, jqXHR) {
if(window.localStorage){
localStorage.vendorID = data;
}
},
...
})
关于javascript - 如何将新创建的ID放入LocalStorage?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38291940/