0

当我使用 Telerik UI 编辑器时,我在 ASP Net Core Web 应用程序中面临 CRUD 操作,创建带有一些格式的示例文本,它很好地存储在 DB 中。但是,如果我想再次编辑此文本,telerik 编辑器将使用 html 标签显示它并且不进行格式化。有人有同样的问题吗?

这是我的代码:

控制器

 public async Task<IActionResult> Edit(int? id)
    {
        if (id == null)
        {
            return NotFound();
        }

        var editorData = await _context.Prescriptions.FindAsync(id);
        if (editorData == null)
        {
            return NotFound();
        }
        return View(editorData);
    }

    [HttpPost]
    [ValidateAntiForgeryToken]
    public async Task<IActionResult> Edit(int id, [Bind("PrescriptionID,PrescriptionText")] Prescription editorData)
    {
        if (id != editorData.PrescriptionID)
        {
            return NotFound();
        }

        if (ModelState.IsValid)
        {
            try
            {
                _context.Update(editorData);
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!EditorDataExists(editorData.PrescriptionID))
                {
                    return NotFound();
                }
                else
                {
                    throw;
                }
            }
            return RedirectToAction(nameof(Index));
        }
        return View(editorData);
    }

和查看

@model WebApplication3.Models.Prescription

<h4>EditorData</h4>
<hr />
<div class="row">
    <div class="col-md-4">
        <form asp-action="Edit" id="EditorDataForm">
            <div asp-validation-summary="ModelOnly" class="text-danger"></div>
            <input type="hidden" asp-for="PrescriptionID" />
            <div class="form-group">
                <label asp-for="PrescriptionText"></label>

                @Html.Kendo().EditorFor(m => m.PrescriptionText).Encoded(false)

                <span asp-validation-for="PrescriptionText" class="text-danger k-invalid-msg" data-for="EditorContent"></span>
            </div>
            <div class="form-group">
                <input type="submit" value="Save" class="k-button k-primary" />
            </div>
        </form>
    </div>
</div>

<div>
    <a asp-action="EditorContent" class="k-button">Back to List</a>
</div>

当我运行并创建新的它工作

创造

但是如果我想编辑它会像这样显示 编辑

4

1 回答 1

0

我试过你的代码,它对我有用。您可以尝试我的示例代码,如果它不起作用,则意味着您的项目中遗漏了一些东西。

首先,我建议您需要在

 var editorData = await _context.Prescriptions.FindAsync(id);

这条线在public async Task<IActionResult> Edit(int? id)方法中。你需要检查editorData.PrescriptionText. 它应该如下所示。

<p>Hello world</p><p>My Sample</p><p><strong><em>Italic <span style="text-decoration:underline;">Underline</span></em></strong></p>

其次,您可以尝试我的示例代码来检查项目中的配置|静态文件。


我的 HomeController 代码:

using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace TelerikAspNetCoreApp1.Controllers
{
    public class HomeController : Controller
    {
        public IActionResult Index()
        {
            Prescription a = new Prescription();
            string aa = "<p>Hello world</p><p>My Sample</p><p><strong><em>Italic <span style=\"text-decoration:underline;\">Underline</span></em></strong></p>";
            int id = 1;
            a.PrescriptionText = aa;
            return View(a);
        }
        public string edit = string.Empty;
        public IActionResult Edit()
        {
            Prescription a = new Prescription();
            a.PrescriptionText = HttpContext.Session.GetString("key");
            return View(a);
        }
        [HttpPost]
        [ValidateAntiForgeryToken]
        public async Task<IActionResult> Edit(int id, [Bind("PrescriptionID,PrescriptionText")] Prescription editorData)
        {
            if (id != editorData.PrescriptionID)
            {
                return NotFound();
            }

            if (ModelState.IsValid)
            {
                string aa = editorData.PrescriptionText;
                HttpContext.Session.SetString("key", aa);
                return RedirectToAction(nameof(Edit));
            }
            return View(editorData);
        }
        public IActionResult About()
        {
            ViewData["Message"] = "Your application description page.";

            return View();
        }

        public IActionResult Contact()
        {
            ViewData["Message"] = "Your contact page.";

            return View();
        }

        public IActionResult Error()
        {
            return View();
        }
    }
}

Edit.cshtml 和 Index.cshtml 是一样的

@{
    ViewData["Title"] = "Home Page";
}
@model TelerikAspNetCoreApp1.Prescription;

<h4>EditorData</h4>
<hr />
<div class="row">
    <div class="col-md-4">
        <form asp-action="Edit" id="EditorDataForm">
            <div asp-validation-summary="ModelOnly" class="text-danger"></div>
            <input type="hidden" asp-for="PrescriptionID" />
            <div class="form-group">
                <label asp-for="PrescriptionText"></label>

                @Html.Kendo().EditorFor(m => m.PrescriptionText).Encoded(false)

                <span asp-validation-for="PrescriptionText" class="text-danger k-invalid-msg" data-for="EditorContent"></span>
            </div>
            <div class="form-group">
                <input type="submit" value="Save" class="k-button k-primary" />
            </div>
        </form>
    </div>
</div>

<div>
    <a asp-action="EditorContent" class="k-button">Back to List</a>
</div>

处方.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace TelerikAspNetCoreApp1
{
    public class Prescription
    {
        public string PrescriptionText { get; set; }
        public int PrescriptionID { get; set; }
    }
}

测试结果:

在此处输入图像描述

于 2021-12-14T07:28:48.580 回答