First, we need to introduce the json script at the front end to facilitate js Object serialization.
<Script type = "text/javascript" src = "/js/jquery. json-2.4.min.js"> </script>
Then we declare a class in the foreground, put the value you want to save into the class, and serialize it finally.
Copy codeThe Code is as follows:
Function Save (){
Var examId = '<% = ExamId %> ';
Var yearTerm = $ ("# <% = DDLYearTerm. ClientID %>"). val ();
Var examType = $ ("# <% = DDLExamType. ClientID %>"). val ();
Var examDate = $ ("# ExamDate"). val ();
Var examName = $ ("# ExamName"). val ();
Var exam = {};
Exam ["ExamId"] = examId;
Exam ["YearTerm"] = yearTerm;
Exam ["ExamType"] = examType;
Exam ["ExamDate"] = examDate;
Exam ["ExamName"] = examName;
Var json = $. toJSON (exam );
Var Result = AjaxController. EditExam (json). value;
If (Result = "Success ")
{
Alert ("saved successfully ");
Parent. $. fancybox. close ();
}
Else
{
Alert (Result );
}
}
Then we perform deserialization in the background and use the value. because we use ajax, we need to add [Ajax. ajaxMethod] feature, and add Ajax registration to the cs on the page where your front-end is located. for more information, see
The use of Microsoft ajax Library (ajax. ajaxMethod) http://www.jb51.net/article/40764.htm
Copy codeThe Code is as follows:
Protected void Page_Load (object sender, EventArgs e)
{
Ajax. Utility. RegisterTypeForAjax (typeof (Youjiao. xxt. BLL. Controller. AjaxController ));
If (! IsPostBack)
{
Databind ();
}
}
Copy codeThe Code is as follows:
[Ajax. AjaxMethod]
Public string EditExam (string value)
{
String Result = "";
Try
{
If (HttpContext. Current. Request. IsAuthenticated)
{
EditExam editExam = JsonSerializeHelper. DeserializeFromJson <EditExam> (value );
ExamController eController = new ExamController ();
EController. EditExam (editExam );
Result = "Success ";
}
Else
{
Result = "the session is invalid. Please log on again! ";
}
}
Catch (Exception ex)
{
Result = ex. Message;
}
Return Result;
}
Image:
Copy codeThe Code is as follows:
[Serializable]
Public class EditExam
{
Public string ExamId {get; set ;}
Public string YearTerm {get; set ;}
Public string ExamType {get; set ;}
Public string ExamDate {get; set ;}
Public string ExamName {get; set ;}
}
In this way, you can avoid passing in a large number of parameters in front-end js, And the backend can also be directly deserialized into a class to point out the Member values.