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'
}
<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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With