首先,我们要在前台引入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里面传入大量的参数,后台也可以直接反序列化为类来点出成员的值了

标签:
json,传递数据,后台

免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
桃源资源网 Design By www.nqtax.com