CKEditor数据在asp.net核心mvc中绑定(CKEditor Data Bind in asp.net core mvc)

编程入门 行业动态 更新时间:2024-10-18 06:04:15
CKEditor数据在asp.net核心mvc中绑定(CKEditor Data Bind in asp.net core mvc)

我已经创建了一个EditController来获取和编辑asp.net核心项目中我的数据库的内容。 控制器如下:

[HttpPost] [ValidateAntiForgeryToken] public async Task<IActionResult> Edit(MyObject model) { if (ModelState.IsValid) { await _myObjectRepository.UpdateMsg(model); return RedirectToAction("Index"); } return View(); }

我使用了以下脚本标记来引用ckeditor并将其保存在Edit.cshtml上。

在表单标签内部,我放置了以下几行代码来显示编辑器,并在编辑器中显示数据库中的数据。

<div class="form-group"> <label asp-for="Body" class="col-sm-2 control-label"></label> <div class="col-sm-10"> <textarea asp-for="Body" id="editor1" name="editor1"></textarea> </div> </div> <script type="text/javascript">CKEDITOR.replace('Body');</script>

我没有问题将数据从数据库显示到编辑器。 但是,当我尝试在编辑器中更改文本时,数据库中不存在文本。

因此,在不使用编辑器的情况下测试了代码,即我只是使用标记而不是CKEditor,并使用通常的剃刀属性,并且它工作正常。 我该如何解决这个问题?

I've created a EditController to fetch and edit the contents of my database in asp.net core project. The Controller is given below:

[HttpPost] [ValidateAntiForgeryToken] public async Task<IActionResult> Edit(MyObject model) { if (ModelState.IsValid) { await _myObjectRepository.UpdateMsg(model); return RedirectToAction("Index"); } return View(); }

I've used the following script tag to reference the ckeditor and kept it on Edit.cshtml.

Inside the form tag I've put the following lines of code to display the editor and display the data from the database inside the editor.

<div class="form-group"> <label asp-for="Body" class="col-sm-2 control-label"></label> <div class="col-sm-10"> <textarea asp-for="Body" id="editor1" name="editor1"></textarea> </div> </div> <script type="text/javascript">CKEDITOR.replace('Body');</script>

I've no problem displaying the data from database to the editor. But as I try to change the text in the editor, no text would be present in the database.

So tested the code without using the editor i.e. I simply used tag instead of the CKEditor, with usual razor attributes and it worked fine. How can I solve this?

最满意答案

我相信你在MyObject类中有一个属性来存储CKEditor的内容。 让我们假设“ckEditorVar”是存储ckeditor内容的属性。 以下代码适用于我。 我希望它也适合你。

[HttpPost] [ValidateAntiForgeryToken] public async Task<IActionResult> Edit(MyObject model) { if (ModelState.IsValid) { await _myObjectRepository.UpdateMsg(new MyObject { ckeditorVar = Request.Form["editor1"].ToString(), }); return RedirectToAction("Index"); } return View(); }

I believe you have a property in the MyObject class to store the content of the CKEditor. Lets suppose "ckEditorVar" is the property to store the ckeditor content. Following code works for me. I hope it will work for you too.

[HttpPost] [ValidateAntiForgeryToken] public async Task<IActionResult> Edit(MyObject model) { if (ModelState.IsValid) { await _myObjectRepository.UpdateMsg(new MyObject { ckeditorVar = Request.Form["editor1"].ToString(), }); return RedirectToAction("Index"); } return View(); }

更多推荐

本文发布于:2023-07-16 12:55:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1128663.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:绑定   核心   数据   net   asp

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!