Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Apr 24th, 2012  |  syntax: None  |  size: 3.34 KB  |  hits: 23  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. MVC3 model DisplayFormat and JSON
  2. [DisplayFormat(DataFormatString = "{0:P2}")]
  3. public Nullable<double> Yearly_act_pct { get; set; }
  4.        
  5. @Html.DisplayFor(modelItem => item.Yearly_act_pct)
  6.        
  7. return Json(myObjects, JsonRequestBehavior.AllowGet);
  8.        
  9. JavaScriptSerializer serializer = new JavaScriptSerializer();
  10. response.Write(serializer.Serialize(Data));
  11.        
  12. myObjects.Yearly_act_pct = Math.Round(myObjects.Yearly_act_pct, 2); // provided you wanted to round to nearest two fractional digits
  13. return Json(myObjects, JsonRequestBehavior.AllowGet);
  14.        
  15. [DataMember(Name = "price", Order = 23)]
  16.     [XmlElement("price", Order = 23)]
  17.     public string Price_String
  18.     {
  19.         get
  20.         {
  21.             return Formatter.FormatAsCurrency(this.Price);
  22.         }
  23.         set
  24.         {
  25.             this.Price = Formatter.ParseCurrency(value);
  26.         }
  27.     }
  28.  
  29.     [XmlIgnore]
  30.     public decimal Price { get; set; }
  31.        
  32. public static string FormatAsCurrency(decimal? amount)
  33.     {
  34.         return amount.HasValue ? String.Format("{0:C}USD", amount).Replace("$","") : null;
  35.     }
  36.     public static decimal ParseCurrency(string value)
  37.     {
  38.         return !String.IsNullOrEmpty(value) ? decimal.Parse(value.Replace("USD", "")) : 0;
  39.     }
  40.     public static decimal? ParseNullableCurrency(string value)
  41.     {
  42.         return !String.IsNullOrEmpty(value) ? decimal.Parse(value.Replace("USD", "")) as decimal? : null;
  43.     }
  44.        
  45. public ContentResult GetContentResult(object responseObject)
  46.     {
  47.         #region Output Format
  48.         if (this.ResponseFormat == ResponseFormatEnum.JSON)
  49.         {
  50.             string json = "";
  51.  
  52.             try
  53.             {
  54.                 System.Runtime.Serialization.Json.DataContractJsonSerializer jsonserializer = new System.Runtime.Serialization.Json.DataContractJsonSerializer(responseObject.GetType());
  55.                 MemoryStream ms = new MemoryStream();
  56.                 jsonserializer.WriteObject(ms, responseObject);
  57.                 json = Encoding.Default.GetString(ms.ToArray());
  58.                 ms.Close();
  59.                 ms.Dispose();
  60.                 jsonserializer = null;
  61.             }
  62.             catch(System.Exception ex)
  63.             {
  64.                 string err = ex.Message;
  65.             }
  66.  
  67.             return new ContentResult() { Content = json, ContentType = "application/json" };
  68.  
  69.         }
  70.         else
  71.         {
  72.             string xml = "";
  73.             try
  74.             {
  75.                 MemoryStream ms = new MemoryStream();
  76.                 System.Xml.Serialization.XmlSerializer serializer = new System.Xml.Serialization.XmlSerializer(responseObject.GetType());
  77.                 System.Xml.Serialization.XmlSerializerNamespaces ns = new System.Xml.Serialization.XmlSerializerNamespaces();
  78.                 ns.Add("", "");
  79.                 ms = new MemoryStream();
  80.                 serializer.Serialize(ms, responseObject, ns);
  81.                 xml = Encoding.Default.GetString(ms.ToArray()); ms.Close();
  82.                 ms.Dispose();
  83.                 serializer = null;
  84.             }
  85.             catch (System.Exception ex)
  86.             {
  87.                 throw ex;
  88.             }
  89.  
  90.             return new ContentResult() { Content = xml, ContentType = "text/xml" };
  91.         }
  92.         #endregion
  93.     }
  94.        
  95. public ActionResult Feed()
  96.     {
  97.         ViewModels.API.Deals.Response response = new ViewModels.API.Deals.Get();
  98.         return GetContentResult(response);            
  99.     }