0
 <div>
Email: 
<a id="email href="mailto:@Model.Contact.Email">@Model.Contact.Email.ToStringMyCustomFormatted</a>
</div>

I want to create custom method for formatting and apply it like this. (in the same way we can apply ToString() method to this.) I don't want to use JavaScript to do any formatting by using Document.Ready(). In short I want to extend ToString method something like ToStringMyCustomFormatted, by which I can apply my own rules to string output. I am not even sure if something like can be done. Please Enlighten !

1 Answer 1

1

You could use a simple C# extension method (or directly on your Contact model):

public static class YourContactModelExtensions {
    public static string ToStringMyCustomFormat(this YourContactModel m) {
        // TODO
    }
} 

or if you wanted it to apply to all strings:

public static class StringExtensions {
    public static string ToMyCustomFormat(this string s) {
        // TODO
    }
} 

or a Razor helper function on your Razor page:

@helper ToStringMyCustomFormat(YourContactModel m) {
   @* TODO *@
}

or

@helper ToMyCustomFormat(string s) {
   @* TODO *@
}
Sign up to request clarification or add additional context in comments.

2 Comments

Just one problem: the extension should be to String not to Contact, since the method is being called off of the Email property, not the instance directly.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.