Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HTMLString Serialized as string, encoded in json

Simply I want to have a property in my object serialized as a string instead of HtmlString(which it is).

The reasoning here is that the object is serialized in json, and the toString function is not returning the string contents of the object but rather '[object Object]'.

Alternatively, would it be possible to label the property as [Non-Serializable] and expose another property in its stead as the serializable version?

Thanks in advance

Edit: Example Code:

[Serializable]
public MyObject 
{
    public int id= 0;
    public string name = "myName";
    public HtmlString WishIWasAString = new HtmlString("notAString");
    public string fakeHtmlString
    {
        get { return WishIWasAString.ToString(); }
    }
}

Example Json:

{
  id: 0, 
  name: 'myName'
  wishIWasAString: {}
  fakeHtmlString: 'notAString'
}
like image 878
Highstead Avatar asked Nov 13 '22 16:11

Highstead


1 Answers

<script type="type/javascript">
    var fooProperty = @Html.Raw(Json.Encode(Model.Foo));
</script>

or if you wanted to JSON serialize your entire model into a javascript variable:

<script type="type/javascript">
    var model = @Html.Raw(Json.Encode(Model));
    alert(model.Foo.Bar);
</script>
like image 78
Darin Dimitrov Avatar answered Nov 16 '22 23:11

Darin Dimitrov