4

I'm currently using EpiServer 7.5 with ASP.Net MVC.

Basically, I'm defining a property inside a model which needs to be of type XHtmlString. What I wish to do inside the model is set a default value for the XHtmlString, but since this is represented by a string, how can I do this?

Here's an example in code - this is my model:

[Required]
[Display(
    Name = "Thank you message",
    Order = 1)]
public virtual XhtmlString ThankYouMessage{ get; set; }

Now, later on the code, I simply want to set the default value of this ThankYouMessage type to a string.

ThankYouMessage = "Default thank you message";

This won't work because ThankYouMessage is an object type of XHtmlString and I obviously can't set this to a type of string.

I think it has something to do with the XHtmlStringConverter but I can't figure this out - any help would be appreciated.

Cheers!

kloarubeek
  • 2,706
  • 20
  • 24
user3556163
  • 114
  • 1
  • 10

1 Answers1

8

You'll need to convert the string into an XhtmlString as that is the property type. This is simply done by creating a new XhtmlString object.

   ThankYouMessage  = new XhtmlString("Default thank you message")
jskzyb
  • 151
  • 3
  • Can you explain why this would work instead of the way the OP tried? – John Odom Jan 12 '15 at 16:45
  • 1
    @JohnOdom The OP's attempt fails because there is no implicit conversion from string to `XhtmlString`. An `XhtmlString` must be created explicitly. – xr280xr Jun 05 '18 at 23:17