vendredi 10 mars 2017

DeserializeObject or Cast object from controller?

I have a Controller that deserialize the request in the following way;

public ActionResult ControlPartial() {
           Dictionary<string, object> modelState = (Dictionary<string, object>)new JavaScriptSerializer().DeserializeObject(Request.Params["ControlState"]); 
            SomeObject model = ControlModelFactory.CreateInstance(modelState );
            return PartialView("ControlPartial", model);
        }


 public static class ControlModelFactory{
         public static SomeObject CreateInstance(Dictionary<string, object> controlState){
           //Here is the Question! How I can deserialize or cast the controlState to SomeObject.
           return new SomeObject(){....};
             }
}

public class SomeObject{
 public int Width { get; set; }
 public Diccionari<string,Serie>{ get; set; }
}

public class Serie{
    public int ViewType { get; set; }
}

In my Request.Params["ControlState"] I have;

  • [0] {[Width, 0]} System.Collections.Generic.KeyValuePair
  • [1] {[Series,System.Collections.Generic.Dictionary`2[System.String,System.Object]]} System.Collections.Generic.KeyValuePair

     And Inside of the Series the following Value; 
     +        [0] {[serie1,System.Collections.Generic.Dictionary`2[System.String,System.Object]]} System.Collections.Generic.KeyValuePair<string,object>
                     +      [0] {[ViewType, 9]} System.Collections.Generic.KeyValuePair<string,object>
    
     +        [1] {[serie2,System.Collections.Generic.Dictionary`2[System.String,System.Object]]} System.Collections.Generic.KeyValuePair<string,object>
                     +      [1] {[ViewType, 11]}    System.Collections.Generic.KeyValuePair<string,object>
    
    

As you can see, from the client I get the JSON: {'Width':0, 'Series':{'serie1':{'ViewType':9},'serie2':{'ViewType':11}}}

What is the best method and how to convert from the controlstate to the object SomeObject? One problem is that I can not change the input parameter types.And I have to do the conversion inside CreateInstance.

Aucun commentaire:

Enregistrer un commentaire