Tuesday, June 30, 2015

example conversion from json string passed to c#
Assume we have a class names Spec and and array of Specs is passed from javascript. Then convert as follows:


        [ValidateInput(false)]
        [HttpPost]
        public JsonResult UpdateSpecs(int SkuID, string Specs)
{

Spec[] args = (Spec[])JsonConvert.DeserializeObject(Specs);

}


The calling Javascript code was as follows:


        var dreq = $.ajax({
            url: "/Create/UpdateSpecs",
            cache: false,
            data: { "SkuID": PID, "Specs": JSON.stringify(aspecs)

            },
            type: "POST"
        });


Another One:

* Server-side:

       [ValidateInput(false)]
        [HttpPost]
        public JsonResult Create(string NewPriceAsJson)
        {
            NewPrice Depo = (NewPrice)JsonConvert.DeserializeObject(NewPriceAsJson);
            Depo.CommandName = CommandType.Insert;
            PriceBinder Binder = new PriceBinder();
            return Json(Binder.Create(Depo));
        }


         [ValidateInput(false)]
        [HttpPost]
        public JsonResult UpdatePPrice(string NewPriceAsJson)
        {
            NewPrice Depo = (NewPrice)JsonConvert.DeserializeObject(NewPriceAsJson);
            Depo.CommandName = CommandType.Edit;
            Depo.isDigital = false;
            PriceBinder Binder = new PriceBinder();
            return Json(Binder.Create(Depo)); // CommandType.Edit
        }

* Client-side:
    function pricePCreate(endPoint, price, fromd, tod) {
        showWait();
        var request = $.ajax({
            url: "/Price/" + endPoint,
            cache: false,
            data: {
                "NewPriceAsJson": JSON.stringify({
                    "PID": $("#PID").html(), "isDigital": false, "SiteDefID": $("#SelectedSite").val(),
                    "price": price, "effectiveFromDate": fromd, "effectiveToDate": tod, "ShippingClassID": $("#shipClassID").val(),
                    "ScheduledTaskSKUId": $("#speditID").html()
                })
            },
            type: "POST"
        });



* The Model:

    [Serializable]
    public enum CommandType
    {
        Insert=1,
        Edit=2
    }

    // passed from UI
    [Serializable]
    public class NewPrice
    {
        public int PID;
        public CommandType CommandName;
        public bool isDigital;
        public int SiteDefID;
        public int ShippingClassID;
        public string effectiveFromHour;
        public string effectiveFromMinute;
        public string effectiveToHour;
        public string effectiveToMinute;
        public DateTime effectiveFromDate;
        public DateTime effectiveToDate;
        public string price;
        public string publisher;
        public string revShare;
        public string msrp;
     

        public string DefaultPrice;
        public string DefaultPublisherPrice;
        public string DefaultMSRP;
        //public string txtRevenueShare;
       

       
       
        public string currency;
        public string headbandText;
        public string designText;
        public string description;
        public bool isonSale;

        public int ScheduledTaskSKUId;

        public string[] AuthFiles;
        public string AuthId;
        public string[] AuthGui;
     //   public bool sponsored;

       
    }