Reputation: 3158
I need to create an attribute which should result in a text area to be a rich text editor (TinyMCE/CKEditor). How would I achieve it?
For example, my model is:
public class MyModel
{
[RichText]
public string Detail {get;set;}
}
When it gets rendered, it should show a rich text area.
Upvotes: 1
Views: 549
Reputation: 7584
You can inherit the DataTypeAttribute
:
public class RichTextAttribute : DataTypeAttribute
{
public RichTextAttribute() : base("RichText") {}
}
You can even add validation in to this attribute if for example you want to prevent certain HTML tags so you can avoid XSS attacks or something.
As stated in another answer, you then simply add an editor template for RichText.cshtml or RichText.ascx in the View\Shared\EditorTemplates folder and in the view where you are showing the property use:
Html.EditorFor(m => m.Detail)
In your editor template is where you would add your code to create a TextArea
(or however you are invoking TinyMCE) and then you can register some script in your master page to attach TinyMCE to whatever tag your editor template outputs.
Upvotes: 1
Reputation: 93494
Assuming you're using at least MVC2, use [UIHint("RichText")]
to indicate you want it to be a RichText field, then create an EditorTemplate called RichText.cshtml (or .aspx, or whatver view technology you're using) and define the HTML you want to use (you will have to create the html to use whatever rich text editor you're using).
Then use EditorFor() in your view, and it should work.
You could also use [DataType(DataType.Html)]
and then create an Html.cshtml EditorTemplate.
Upvotes: 3