我在MVC 3应用程序中使用CKeditor。
现在我需要将带有HTML标签的文本保存到DB,问题是Ckeditors GetData()方法返回数据的原始格式(http://docs.cksource.com/ckeditor_api/symbols/CKEDITOR.editor.html#getData)
我需要一种方法将原始格式化为带有Html标记的普通字符串
查看:
@using (Html.BeginForm()) {
@Html.ValidationSummary(true)
<fieldset>
<legend>TextEditionViewModel</legend>
@Html.DropDownListFor(model=>model.Id, Model.Texts)
<div class="editor-label">
@Html.LabelFor(model => model.Text)
</div>
<div class="editor-field">
@Html.TextAreaFor((model => model.Text), new { @Id = "editor1" })
@Html.ValidationMessageFor(model => model.Text)
</div>
<script type="text/javascript">
CKEDITOR.replace('editor1');
</script>
<script type="text/javascript">
var editor_data = CKEDITOR.instances['editor1'].getData();
var url = '@Url.Action("EditText1", "Admin")';
var data = { CommentText: editor_data };
function Save() {
alert(editor_data);
$.post(url, { CommentText: editor_data }, function (result) {
});
};
$('#Id').change(function () {
var selectedText = $(this).val();
if (selectedText != null && selectedText != '') {
$.getJSON('@Url.Action("Text","Admin")', { Id: selectedText }, function (text) {
CKEDITOR.instances['editor1'].setData(text);
});
}
});
</script>
</fieldset>
<p>
<input type="button" value="Save" onclick="Save()"/>
</p>
}
如果editor_data设置为Normal“Text”字符串,一切正常!
控制器
public ActionResult EditText1(String CommentText)
{
return null;
}
答案 0 :(得分:1)
您可以使用[ValidateInput]
属性修饰控制器操作,以允许POST默认情况下未经ASP.NET授权的HTML标记:
[HttpPost]
[ValidateInput(false)]
public ActionResult EditText1(string commentText)
{
return null;
}
更新:
问题与Save
方法无法访问editor_data变量有关。
你可以这样做:
<script type="text/javascript">
function Save() {
var editor_data = CKEDITOR.instances['editor1'].getData();
var url = '@Url.Action("EditText1", "Admin")';
var data = { CommentText: editor_data };
$.post(url, data, function (result) {
});
}
...
</script>