/// <summary> /// Method used to deserialize the NavData information from NavigationInfo path /// in the form object. This is will help us to restore te modified information /// while performing the submission of NavigationForm through javascript. /// </summary> /// <param name="request"></param> /// <param name="navigationInfo"></param> private void UpdateNavigationData(HttpRequestBase request, NavigationInfo navigationInfo) { var navigationData = request.Form["navigateInfo"]; if (string.IsNullOrEmpty(navigationData)) { return; } var jTokenNavigationData = JToken.Parse(navigationData); //NewtonSoft will consider every object as JProperty. //This will be interated to match with the property in NavigationInfo type. //If NavigationInfo class has any matched properties, then we will set the //value from JProperty to corresponding type in NavigationInfo using reflection. foreach (JProperty jTokenData in jTokenNavigationData) { var property = navigationInfo.GetType().GetProperty(jTokenData.Name, BindingFlags.IgnoreCase | BindingFlags.Public | BindingFlags.Instance); if (property == null) { continue; } if (jTokenData.Value is JValue) { var value = ((JValue)jTokenData.Value).Value; //In case of Nullable properties, we need to consider taking the Underlying property type //to set the data. Otherwise, reflection will throw error during setValue. var propertyType = Nullable.GetUnderlyingType(property.PropertyType) ?? property.PropertyType; property.SetValue(navigationInfo, Convert.ChangeType(value, propertyType), null); } else { property.SetValue(navigationInfo, (dynamic)jTokenData.Value, null); } } }
/// <summary> /// Method used to deserialize the NavData information from NavigationInfo path /// in the form object. This is will help us to restore te modified information /// while performing the submission of NavigationForm through javascript. /// </summary> /// <param name="request"></param> /// <param name="navigationInfo"></param> private void UpdateNavigationData(HttpRequestBase request, NavigationInfo navigationInfo) { var navigationData = request.Form["navigateInfo"]; if (string.IsNullOrEmpty(navigationData)) return; var jTokenNavigationData = JToken.Parse(navigationData); //NewtonSoft will consider every object as JProperty. //This will be interated to match with the property in NavigationInfo type. //If NavigationInfo class has any matched properties, then we will set the //value from JProperty to corresponding type in NavigationInfo using reflection. foreach (JProperty jTokenData in jTokenNavigationData) { var property = navigationInfo.GetType().GetProperty(jTokenData.Name, BindingFlags.IgnoreCase | BindingFlags.Public | BindingFlags.Instance); if (property == null) continue; if (jTokenData.Value is JValue) { var value = ((JValue)jTokenData.Value).Value; //In case of Nullable properties, we need to consider taking the Underlying property type //to set the data. Otherwise, reflection will throw error during setValue. var propertyType = Nullable.GetUnderlyingType(property.PropertyType) ?? property.PropertyType; property.SetValue(navigationInfo, Convert.ChangeType(value, propertyType), null); } else { property.SetValue(navigationInfo, (dynamic)jTokenData.Value, null); } } }