曾在一个论坛项目中,需要在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)]的定义才能使文本编辑器的值顺利保存。