Ajax提交Json字串,.net還原序列化成Model對象

來源:互聯網
上載者:User

1.建立Model對象

/// <summary>    /// 使用者實體類    /// </summary>    public class UserModel    {        /// <summary>        /// 使用者ID        /// </summary>        public long UserID { get; set; }        /// <summary>        /// 使用者顯示名        /// </summary>        public string DisplayName { get; set; }        /// <summary>        /// 使用者名稱        /// </summary>        public string RealName { get; set; }    }

2.建立Json序列化協助類

/* ============================================================================== * 功能描述:JsonHelper   * 創 建 者:RES-WUZHOU * 建立日期:2012/6/21 17:17:08 * ==============================================================================*/using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Runtime.Serialization.Json;using System.IO;using System.Web.Script.Serialization;namespace Common{    /// <summary>    /// JsonHelper    /// </summary>    public class JsonHelper    {        /// <summary>        /// JSON序列化        /// </summary>        /// <typeparam name="T"></typeparam>        /// <param name="entity"></param>        /// <returns></returns>        public static string JsonSerializer<T>(T entity)        {            var serializer = new JavaScriptSerializer();            return serializer.Serialize(entity);        }        /// <summary>        /// JSON還原序列化        /// </summary>        /// <typeparam name="T"></typeparam>        /// <param name="jsonString"></param>        /// <returns></returns>        public static T JsonDeserialize<T>(string jsonString)        {                        var serializer = new JavaScriptSerializer();            return serializer.Deserialize<T>(jsonString);        }    }}

注意:手動引用 System.Web.Script.Serialization.dll

3.後台 CS方法

 public ActionResult Add(string strUserModel)        {            UserModel userModel = JsonHelper.JsonDeserialize<UserModel>(strUserModel);    //....        }    

4.前台ajax提交

var userModel = {};userModel.UserID=1;userModel.DisplayName = "張三";userModel.RealName= "zhang";$.ajax({                url: "/user/add",                type: "post",                data: { strUserModel: $.toJSON(userModel) },                success: function (json) {                                         alert('成功');                },                error: function () {                    alert("系統異常!");                }            });

注意:頁面需引用jquery.js和jquery.json.js

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.