小弟剛接觸MVC3.0,雖然已有幾年web開發,也使用過fckeditor,xheditor,freetext,kindeditor等html在線編輯器。
但是在MVC環境下還沒有使用過。今天自己折騰了好一會才解決。項目中使用的是kindeditor.
第一步:首先要有kindeditor,官網下載地址:http://www.kindsoft.net/down.php,目前最新版本是4.0.5,更新時間是2012.1.15
文件有600多k,但是實際使用只需要其中幾個文件。解壓文件后,copy 根目錄2個js文件,以及themes(放的樣式、圖片)文件夾,plugins文件夾,lang文件夾中是語言,我們只需要其中的zh_CN.js。最后放項目中的文件如下截圖:
第二步:引入js文件,初始化編輯器。對於細化編輯器的可以參考官網demohttp://www.kindsoft.net/demo.php
<script src="@Url.Content("~/Scripts/kindeditor/kindeditor.js")" type="text/javascript"></script> <script src="@Url.Content("~/Scripts/kindeditor/lang/zh_CN.js")" type="text/javascript"></script> <script type="text/javascript"> var editor; KindEditor.ready(function (K) { editor = K.create('textarea[name="Information"]', { allowFileManager: true }); }); </script>
第三步:使用KindEditor
@Html.TextAreaFor(model => model.Information, new { style="width:800px;height:400px"})
第四步:Controller,要設置ValidateInput false,不然有html標簽會報錯的。
[HttpPost] [ValidateInput(false)] public ActionResult Create(NewsEntity news) { if (ModelState.IsValid) { news.Time = DateTime.Now; PE.NewsEntity.Add(news); try { PE.SaveChanges(); return RedirectToAction("News"); } catch (Exception e) { throw e; } } return View(); }
最后在頁面上效果圖: