首先,我们要在前台引入json的脚本,以便于把js对象序列化

<script type="text/javascript" src="/UploadFiles/2021-04-02/jquery.json-2.4.min.js">

然后我们在前台声明一个类,将你要保存的值放到类里面,最后序列化

复制代码 代码如下:
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("保存成功");
                parent.$.fancybox.close();
            }
            else
            {
                alert(Result);
            }
        }

然后我们在后台,进行反序列化,并使用值.因为我们使用ajax所以要在后台的方法上加上[Ajax.AjaxMethod]特性,并且在你前台所在页面的cs里面也要加上Ajax的注册.具体使用看

微软 ajax 库 的使用方法( ajax.ajaxMethod) https://www.jb51.net/article/40764.htm

复制代码 代码如下:
protected void Page_Load(object sender, EventArgs e)
        {
            Ajax.Utility.RegisterTypeForAjax(typeof(Youjiao.xxt.BLL.Controller.AjaxController));
            if (!IsPostBack)
            {
                Databind();
            }
        }

复制代码 代码如下:
[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 = "会话无效,请重登录!";
                }
            }
            catch (Exception ex)
            {
                Result = ex.Message;
            }
            return Result;
        }

图片:

复制代码 代码如下:
[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; }
    }

这样就可以很好的避免在前台js里面传入大量的参数,后台也可以直接反序列化为类来点出成员的值了

广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!