在 MVC 中使用 FCKEditor 提交表单时出现问题

发布于 2024-10-15 00:00:16 字数 2357 浏览 6 评论 0 原文

我的 MVC 项目中的 FCKEditor 有一些奇怪的问题。

我本质上有一个视图,它呈现包含我的 FCKEditor 的部分视图(javascript、html 和任何其他位,使我的控件可通过我的应用程序重复使用)

我通过执行以下操作来调用 FCKEditor:

<script src="<%= Url.Content("~/Scripts/fckeditor/fckeditor.js") %>" type="text/javascript" ></script>
<script type="text/javascript">
window.onload = function () {
    var sBasePath = '<%= Url.Content("~/Scripts/fckeditor/") %>';

    var oFCKeditor = new FCKeditor('FckEditor1');
    oFCKeditor.BasePath = sBasePath;
    oFCKeditor.ReplaceTextarea();
}
</script>

为了验证我的表单,我使用 jQuery.Validate 和以下代码:

    //FORM VALIDATION
    $("#NewPageForm").validate({
        errorContainer: "#errorblock-div1, #errorblock-div2",
        errorLabelContainer: "#errorblock-div2 ul",
        wrapper: "li",
        rules: {
            titleTxt: "required",
            FckEditor1: "required"
        },
        messages: {
            titleTxt: "You must enter a page title.",
            FckEditor1: "You must enter at least some content."
        },
        submitHandler: function (form) {
            form.submit();
        }
    });

在本地,当调试我的项目时,这绝对可以正常工作,并且当用户将编辑器留空时会向用户抛出错误。

但是,当我发布应用程序并从服务器运行它时,每当编辑器中有内容时,表单都不会验证,告诉我我的 FCKEditor 控件为空,而事实显然不是。

如果我取消验证,并提交填写了所有相关框的表单,则 collection("FckEditor1") 的集合也是空的,但同样,每次在本地运行此操作都是第一次。

我进行了搜索,似乎发生的情况是我的文本区域 <%= Html.TextArea("FckEditor1", New With {.name = "FckEditor1", .class = "required"} )%> 并未填充输入到 FCKEditor 中的内容,即使这是 FCK 链接到的框。

我尝试过的所有浏览器(IE8 和 FF)中都存在该问题。

不确定 FCK 仍然渲染得很好是否也值得注意?

我并不完全相信这是我的代码的问题,因为它在本地运行完美。我需要在服务器端或 web.config 中配置任何可能导致此问题的内容吗?

还有其他人遇到过这个问题或者对如何解决这个问题有任何想法吗?

编辑

我想我可以通过修复来完成。

到目前为止,我已经添加了以下内容:

    $('#publishBtn').button().click(function () {
        if (typeof (FCKeditorAPI) == "object") {

            FCKeditorAPI.GetInstance("FckEditor1").UpdateLinkedField();

        } else {
            alert('this is not an object!');
        }

        if ($("#FckEditor1").val() == "") {
            alert("FCKEditor is empty");
        } else {
            $("#NewPageForm").submit();
        }
    });

不过,提交表单时我现在被发送到空白页面,尽管我的控制器中似乎没有任何问题 - 至少我不认为存在问题在那里,它会在我的本地计算机上执行相同的操作。

I've a bit of an odd issue with FCKEditor in my MVC project.

I've essentially got a View which renders a Partial View containing my FCKEditor (javascript, html and any other bits to make my control reusable throught my app)

I'm calling FCKEditor by doing the following:

<script src="<%= Url.Content("~/Scripts/fckeditor/fckeditor.js") %>" type="text/javascript" ></script>
<script type="text/javascript">
window.onload = function () {
    var sBasePath = '<%= Url.Content("~/Scripts/fckeditor/") %>';

    var oFCKeditor = new FCKeditor('FckEditor1');
    oFCKeditor.BasePath = sBasePath;
    oFCKeditor.ReplaceTextarea();
}
</script>

To validate my form, I'm using jQuery.Validate with the following code:

    //FORM VALIDATION
    $("#NewPageForm").validate({
        errorContainer: "#errorblock-div1, #errorblock-div2",
        errorLabelContainer: "#errorblock-div2 ul",
        wrapper: "li",
        rules: {
            titleTxt: "required",
            FckEditor1: "required"
        },
        messages: {
            titleTxt: "You must enter a page title.",
            FckEditor1: "You must enter at least some content."
        },
        submitHandler: function (form) {
            form.submit();
        }
    });

Locally, when debugging my project, this works absolutely fine and throws up an error to the user when they leave the editor blank.

However, when I publish the application and run it from the server, whenever the editor has content in, the form won't validate, telling me that my FCKEditor control is empty, when it clearly isn't.

If I take away the validation, and submit the form with all the relevant boxes filled, the collection of collection("FckEditor1") is also empty, but again, running this locally works first time, every time.

I've had a search around and what seems to be happening is that my text area <%= Html.TextArea("FckEditor1", New With {.name = "FckEditor1", .class = "required"})%> isn't being populated with the content entered into the FCKEditor, even though this is the box that FCK is linked to.

The issue exists in all the browsers I've tried (IE8 and FF).

Not sure if it's also worth noting that FCK still renders fine too?

I'm not entirely convinced it's an issue with my code as it runs perfect locally. Is there anything I need to configure server-side or in web.config that could be causing it?

Has anyone else come across this or have any ideas as to how to solve the issue?

EDIT

I think I could be partway there with a fix.

I've added the following so far:

    $('#publishBtn').button().click(function () {
        if (typeof (FCKeditorAPI) == "object") {

            FCKeditorAPI.GetInstance("FckEditor1").UpdateLinkedField();

        } else {
            alert('this is not an object!');
        }

        if ($("#FckEditor1").val() == "") {
            alert("FCKEditor is empty");
        } else {
            $("#NewPageForm").submit();
        }
    });

Though, I now get sent to a blank page when the form is submitted, though nothing in my controller seems to be the issue - at least I don't think so as if there was an issue there, it'd do the same on my local machine.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

二智少女 2024-10-22 00:00:16

如果您在服务器上进行验证,可能会导致问题的一件事是,一旦在 IIS 上进行验证,您就无法在没有请求验证阻止 FCKContent 传递的情况下发布 HTML 内容。卡西尼号不会检查这一点,因此它可以在您的本地计算机上运行。

尝试将其添加到您发布验证时调用的操作中:

[ValidateInput(false)] 
public ActionResult UpdateAction() 
{ 

}

One thing that may be causing your issue if you are validating on the server is that once it's on IIS you cannot post HTML content without the request validation preventing the FCKContent from being passed. Cassini wont check this so it works on your local machine.

try adding this to your the action which is called when you post to validate:

[ValidateInput(false)] 
public ActionResult UpdateAction() 
{ 

}
揽清风入怀 2024-10-22 00:00:16

如果可能,请改用CKEditor;它与 MVC 集成得更好。

http://ckeditor.com/

http://ckeditor.com/what-is-ckeditor

If possible, use CKEditor instead; it integrates with MVC much better.

http://ckeditor.com/

http://ckeditor.com/what-is-ckeditor

羁〃客ぐ 2024-10-22 00:00:16

我找到了问题的根源,这与我在控制器中处理多个提交按钮的方式有关。

我已经对其进行了调整,现在已修复。

I found the source of my problem, and it's to do with the way I'm handling multiple submit buttons in my controller.

I've tweaked it and it's now fixed.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文