Use CKEditor01-basics in MVC

Source: Internet
Author: User

This article describes how to use CKEditor in MVC. It is just about the concept and basis, and does not consider the combination of verification and CKEditor.

 

□1 use NUGET to introduce CKEditor
PM> Install-Package CKEditor

 

After introduction, CKEditor files are available in Scripts:

 

□2 View Model

using System.ComponentModel.DataAnnotations;
 
namespace MvcCKEditor.Models
{
    public class Article
    {
        public int ID { get; set; }
        [Required]
[Display (Name = "topic")]
        public string Subject { get; set; }
 
        [Required]
[Display (Name = "content")]
        public string Content { get; set; }
    }
}

 

□3 no matter where the file is referenced, the following js files related to CKEditor must be referenced:
<Script src = "@ Url. Content ("~ /Scripts/ckeditor. js ")" type = "text/javascript"> </script>

 

□4 HomeController

To add a view and receive the added content:
When receiving the added content, it puts all errors in the form of key-value pairs in the Dictionary set by putting Dictionary <string, string>, and then uses Newtonsoft. json converts the set to a json string, and then let jquery judge the result at the front end. However, in actual projects, ModelState may be used for background verification, and the part to be verified may be placed in some views, which is more convenient for the moment.


As in ASP. NET WebForm development, if ValidateInput = false is not set on the page, a warning is displayed. If this parameter is not set in MVC, the following error is reported:

 

CKEditor can be set in two ways:
1. In the Controller Method

        [ValidateInput(false)]
        public ActionResult Create(string subject, string content)

 

2. perform global settings in Scripts/ckeditor/config. js.

CKEDITOR.editorConfig = function( config )
{
    // Define changes to default configuration here. For example:
    // config.language = 'fr';
    // config.uiColor = '#AADC6E';
 
// You can also set global settings here
    //config.htmlEncodeOutput = true;
};   

Controller and method:

Expand System. web. mvc; mvmediaeditor. models; Newtonsoft. json; mvmediaeditor. controllers {HomeController: Controller {List <Article> articles =; HomeController () {articles = List <Article> {Article () {ID = 1, Subject = "", content = ""}, Article () {ID = 2, Subject = "", Content = "" }};} ActionResult Index () {View (articles );} actionResult Create () {View ();} [HttpPost] [ValidateInput ()] ActionResult Create (subject, content) {Dictionary <,> jo = Dictionary <,> (); (. isNullOrEmpty (subject) {jo. add ("", ""); Content (JsonConvert. serializeObject (jo ),"");}(. isNullOrEmpty (content) {jo. add ("", ""); Content (JsonConvert. serializeObject (jo), "") ;}{ Article article = Article (); article. subject = subject; article. content = content; articles. add (article); jo. add ("", "") ;}( Exception ex) {jo. add ("", ""); jo. add ("", ex. message);} Content (JsonConvert. serializeObject (jo ),"");}}}

□5 Create. cshtml View

Get CKEditor content:

You cannot use var content = $ ('# content'). val (); To obtain CKEditor content.
Because the content of TextArea has been replaced by CKEdtor: var editor = CKEDITOR. editor. replace ('content', {skin: 'Kama ', width: '800px '});
Use the following method to obtain the content of CKEditor: var content = editor. getData ();

Expand "; Layout =" ";}< h2> Create 

Result:

 

References:
※Asp. net mvc 3 uses CKEditor

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.