我遇到了困境。我使用ajax将数据传递给控制器,并且控制器有一个方法将数据传递给DAL,DAL返回一个int,即新创建的插入记录的ID。我的问题是如何获取返回的ID,然后将其放入LocalStorage?
这是NewVendorToSubmit
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,而不必再往返数据库呢?
答案 0 :(得分:0)
似乎将供应商的ID作为对POST
请求的响应返回,将其存储在本地存储中,您可以执行此操作:
$.ajax({
...,
success: function (data, textStatus, jqXHR) {
if(window.localStorage){
localStorage.vendorID = data;
}
},
...
})