Đôi khi không đủ các yếu tố code, hay các module đều có một phương thức update một field hoặc nhiều field chỉ định vậy chúng ta sẽ viết một ajax như sau
Bước 1: Viết javascript cho một request
Ở js file tôi viết logic sau
$("#se_survey").on("change", function () {
var commonModule = new CommonModule();
var header = {
id: commonModule.urlParam("id"),
status: $(this).val(),
module: Module,
tab: "TroGiup",
userid: $("#hf_UserId").val()
}
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
dataType: "json",
url: '/Commands/ajax_UpdateSurvey.aspx/UpdateSurvey',
data: JSON.stringify({header: header})
}).done(function (result) {
console.log(result.d);
}).fail(function () {
}); //end ajax
});
Bước 2: xử lý ở server side
//Commands/ajax_UpdateSurvey.aspx
[System.Web.Services.WebMethod]
public static bool UpdateSurvey(object header)
{
string id = string.Empty;
string module = string.Empty;
string status = string.Empty;
string tab = string.Empty;
foreach (KeyValuePair<string, object> item in (Dictionary<string, object>)header)
{
if (item.Key.ToLower() == "module")
{
module = item.Value.ToString();
module = module.Replace("-", string.Empty);
if (module.Equals("CongTy"))
{
module = "LocalCompany";
}
}
else if (item.Key.ToLower() == "status")
{
status = item.Value.ToString();
}
else if (item.Key.ToLower() == "tab")
{
tab = item.Value.ToString();
}
else if (item.Key.ToLower() == "id")
{
id = item.Value.ToString();
}
}
if (!DAO._Modules.SelectById(module).IsEnable)
{
return false;
}
return DAO.ModuleCommonDAO.UpdateAttr(int.Parse(id), new string[][] { new string[] { "KhachDanhGia", status } }, tab);
}
Bước 3: Nhận và xử lý kết quả từ server
Chín là ở js file tại bước 1
Trong ví dụ này tôi in kêt quả bằng console.log
console.log(result.d);
Không có nhận xét nào:
Đăng nhận xét