曾在一個論壇項目中,需要在mvc框架中使用KindEditor來編輯文章,由於當時也是剛接觸MVC框架,也很是頭疼。
后來經過多般努力,找到了適合自己的方法。
首先是利用 @Html.TextArea()來生命一個文本域,在通過JS腳本來將 KindEditor綁定到文本域上,同時也可以通過css樣式來控制編輯器的格式。
1 <script> 2 KindEditor.ready(function (K) { 3 var editor1 = K.create('#content1', { 4 cssPath: 'editor/plugins/code/prettify.css', 5 uploadJson: 'editor/asp.net/upload_json.ashx', 6 fileManagerJson: 'editor/asp.net/file_manager_json.ashx', 7 allowFileManager: true, 8 afterCreate: function () { 9 var self = this; 10 K.ctrl(document, 13, function () { 11 self.sync(); 12 K('form[name=example]')[0].submit(); 13 }); 14 K.ctrl(self.edit.doc, 13, function () { 15 self.sync(); 16 K('form[name=example]')[0].submit(); 17 }); 18 } 19 }); 20 prettyPrint(); 21 }); 22 </script>
1 <td class="td2"> 2 @Html.TextArea("content1", new { Class = "www" })@* 3 @Html.TextAreaFor(mod => mod.AModel.Loginformation, new { Class="www"})*@ 4 @Html.ValidationMessage("nullerror") 5 </td>
1 string textvalue = Request.Params["content1"].ToString();
到Controllers文件中使用Request.Params[].ToString();來取得編輯器的值。
同時Controllers文件中方法,在接收和使用編輯器的值時我們要用 [ValidateInput(false)]在定義當方法,因為VS在MVC中有嚴格的安全驗證,只有通過 [ValidateInput(false)]的定義才能使文本編輯器的值順利保存。