mvc 3 razor 中的tinymce,Ajax.ActionLinks 在第一次 ajax 调用后失败

发布于 2024-12-03 04:16:25 字数 5295 浏览 1 评论 0 原文

我在 asp.net mvc 3 Razor 应用程序中使用 Tinymce。 Ajax.ActionLink 通过调用名为“GetContent”的控制器操作来加载tinymce 编辑器。 GetContent 方法从文件系统加载文本文件。一切都很好。但是,在我通过 $.ajax 调用保存 tinymce 文本后,Ajax.ActionLink 不再触发控制器方法。换句话说,$.ajax 帖子中的某些内容破坏了客户端上的 Ajax.ActionLink,使其不再调用 GetContent 控制器操作。

有趣的是,Ajax.ActionLink 仍然加载tinymce 编辑器,但是从浏览器缓存加载。在下面的示例中,我有 2 个链接“FileOne”和“FileTwo”,它们加载两个不同的文本文件。在我调用 $.ajax 之前,链接会从磁盘加载文件。在我调用 $.ajax 后,链接会从浏览器缓存加载最后一个“FileOne”或“FileTwo”。

这就是观点。 $.ajax 帖子发生在tiny_mce_save_click() 函数内,该函数连接到tinymce 保存按钮单击:

        @model TestTinyMCE.Models.HomeModel
    @{
        ViewBag.Title = "Home Page";
    }
    @section JavaScript
    {
        <script src="@Url.Content("~/Scripts/tiny_mce/jquery.tinymce.js")" type="text/javascript"></script>
        <script src="@Url.Content("~/Scripts/jquery.unobtrusive-ajax.min.js")" type="text/javascript"></script>
        <script type="text/javascript">
            $().ready(function () {
                init_tiny_mce();
            });
            function init_tiny_mce() {
                $('textarea.tinymce').tinymce({
                    // Location of TinyMCE script
                    script_url: '/Scripts/tiny_mce/tiny_mce.js',

                    //javascript function called when tinymce save button is clicked.
                    save_onsavecallback: "tiny_mce_save_click",

                    encoding: "xml",

                    theme: "advanced",
                    plugins: "save",
                    theme_advanced_buttons1: "save",
                    theme_advanced_toolbar_location: "top"
                });
            }
            function tiny_mce_save_click(tinyMceInstance) {
                $.ajax({
                    type: 'POST',
                    url: '/Home/SaveContent',
                    data: $('form').serialize(),
                    success: function (data, status, xml) {
                        $('#results').html(data);
                    },
                    error: function (xml, status, error) {
                        $('#results').html(error);
                    }
                });

                return false;
            }
        </script>
    }
    @using (Html.BeginForm())
    {
        <ul>
            @foreach (string fileName in Model.FileList)
            {
                <li>@Ajax.ActionLink(fileName, "GetContent", new { FileName = fileName }, new AjaxOptions() { UpdateTargetId = "divContent" })</li>
            }
        </ul>

        <div id="divContent">
            @Html.Partial("GetContent", Model)
        </div>
    }

部分视图“GetContent”是:

    @model TestTinyMCE.Models.HomeModel
        @{
            Layout = null;
        }
        <div id="divContent">
            <fieldset id="fsContent">
                <span id="results"></span><legend>Edit Content &nbsp; @Html.DisplayTextFor(m => m.FileName)</legend>
                @Html.TextAreaFor(m => m.Content,
                new Dictionary<string, object>{
                    {"class","tinymce"}, {"cols","80"}, {"rows","10"}}
                    )
                @Html.HiddenFor(m => m.FileName)
            </fieldset>
            @if (@IsAjax)
            {
                <text>
                <script type="text/javascript">init_tiny_mce();</script>
                </text>
            }
        </div>

这是控制器。 $.ajax post 发生后不再调用 GetContent 方法:

    using System;
        using System.Collections.Generic;
        using System.Linq;
        using System.Web;
        using System.Web.Mvc;
        using TestTinyMCE.Models;

        namespace TestTinyMCE.Controllers
        {
            public class HomeController : Controller
            {
                public ActionResult Index()
                {
                    return View(new HomeModel());
                }

                public ActionResult GetContent(HomeModel homeModel)
                {
                    if (!string.IsNullOrEmpty(homeModel.FileName))
                    {
                        string path = string.Format("~/App_Data/{0}.htm", homeModel.FileName);
                        string physicalPath = Server.MapPath(path);
                        if (!System.IO.File.Exists(physicalPath))
                            homeModel.Content = string.Format("The file '{0}' does not exist.", physicalPath);
                        else
                            homeModel.Content = System.IO.File.ReadAllText(physicalPath);
                    }
                    return View(homeModel);
                }

                [HttpPost]
                [ValidateInput(false)]
                public ActionResult SaveContent(HomeModel homeModel)
                {
                    string path = string.Format("~/App_Data/{0}.htm", homeModel.FileName);
                    string physicalPath = Server.MapPath(path);
                    System.IO.File.WriteAllText(physicalPath, homeModel.Content);
                    ViewBag.Result = "The file was successfully saved.";
                    return View();
                }
            }
        }

I am using Tinymce inside an asp.net mvc 3 Razor application. An Ajax.ActionLink loads the tinymce editor via a call to a controller action named "GetContent". The GetContent method loads a text file from the file system. All is well. But, after I save the tinymce text via an $.ajax call, the Ajax.ActionLink no longer fires the controller method. In other words, something in the $.ajax post breaks the Ajax.ActionLink on the client so that it no longer calls the GetContent controller action.

Interestingly, the Ajax.ActionLink still loads the tinymce editor, but from the browser cache. In the example below I have 2 links "FileOne" and "FileTwo", which load two different text files. Before I call $.ajax the links load the file from disk. After I call $.ajax the links load the last "FileOne" or "FileTwo" from the browser cache.

This is the view. The $.ajax post occurs inside the tiny_mce_save_click() function, which is wired to the tinymce save button click:

        @model TestTinyMCE.Models.HomeModel
    @{
        ViewBag.Title = "Home Page";
    }
    @section JavaScript
    {
        <script src="@Url.Content("~/Scripts/tiny_mce/jquery.tinymce.js")" type="text/javascript"></script>
        <script src="@Url.Content("~/Scripts/jquery.unobtrusive-ajax.min.js")" type="text/javascript"></script>
        <script type="text/javascript">
            $().ready(function () {
                init_tiny_mce();
            });
            function init_tiny_mce() {
                $('textarea.tinymce').tinymce({
                    // Location of TinyMCE script
                    script_url: '/Scripts/tiny_mce/tiny_mce.js',

                    //javascript function called when tinymce save button is clicked.
                    save_onsavecallback: "tiny_mce_save_click",

                    encoding: "xml",

                    theme: "advanced",
                    plugins: "save",
                    theme_advanced_buttons1: "save",
                    theme_advanced_toolbar_location: "top"
                });
            }
            function tiny_mce_save_click(tinyMceInstance) {
                $.ajax({
                    type: 'POST',
                    url: '/Home/SaveContent',
                    data: $('form').serialize(),
                    success: function (data, status, xml) {
                        $('#results').html(data);
                    },
                    error: function (xml, status, error) {
                        $('#results').html(error);
                    }
                });

                return false;
            }
        </script>
    }
    @using (Html.BeginForm())
    {
        <ul>
            @foreach (string fileName in Model.FileList)
            {
                <li>@Ajax.ActionLink(fileName, "GetContent", new { FileName = fileName }, new AjaxOptions() { UpdateTargetId = "divContent" })</li>
            }
        </ul>

        <div id="divContent">
            @Html.Partial("GetContent", Model)
        </div>
    }

The partial view "GetContent" is:

    @model TestTinyMCE.Models.HomeModel
        @{
            Layout = null;
        }
        <div id="divContent">
            <fieldset id="fsContent">
                <span id="results"></span><legend>Edit Content   @Html.DisplayTextFor(m => m.FileName)</legend>
                @Html.TextAreaFor(m => m.Content,
                new Dictionary<string, object>{
                    {"class","tinymce"}, {"cols","80"}, {"rows","10"}}
                    )
                @Html.HiddenFor(m => m.FileName)
            </fieldset>
            @if (@IsAjax)
            {
                <text>
                <script type="text/javascript">init_tiny_mce();</script>
                </text>
            }
        </div>

This is the controller. The GetContent method no longer gets called after the $.ajax post occurs:

    using System;
        using System.Collections.Generic;
        using System.Linq;
        using System.Web;
        using System.Web.Mvc;
        using TestTinyMCE.Models;

        namespace TestTinyMCE.Controllers
        {
            public class HomeController : Controller
            {
                public ActionResult Index()
                {
                    return View(new HomeModel());
                }

                public ActionResult GetContent(HomeModel homeModel)
                {
                    if (!string.IsNullOrEmpty(homeModel.FileName))
                    {
                        string path = string.Format("~/App_Data/{0}.htm", homeModel.FileName);
                        string physicalPath = Server.MapPath(path);
                        if (!System.IO.File.Exists(physicalPath))
                            homeModel.Content = string.Format("The file '{0}' does not exist.", physicalPath);
                        else
                            homeModel.Content = System.IO.File.ReadAllText(physicalPath);
                    }
                    return View(homeModel);
                }

                [HttpPost]
                [ValidateInput(false)]
                public ActionResult SaveContent(HomeModel homeModel)
                {
                    string path = string.Format("~/App_Data/{0}.htm", homeModel.FileName);
                    string physicalPath = Server.MapPath(path);
                    System.IO.File.WriteAllText(physicalPath, homeModel.Content);
                    ViewBag.Result = "The file was successfully saved.";
                    return View();
                }
            }
        }

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

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

发布评论

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

评论(1

夏尔 2024-12-10 04:16:25

问题是浏览器缓存。要防止在 Ajax.ActionLink 上进行缓存,您必须添加 AjaxOption HttpMethod = "POST"。在上面的代码中将 ActionLink 更改为

<li>@Ajax.ActionLink(fileName, "GetContent", new { FileName = fileName }, new AjaxOptions() { UpdateTargetId = "divContent", HttpMethod = "POST" })</li>. 

See http://forums.asp.net/t/1681358.aspx?Disable+cache+in+Ajax+ActionLink+extension+method+in+asp+net+MVC

The problem is broswer caching. To prevent caching on the Ajax.ActionLink you must add AjaxOption HttpMethod = "POST". In the above code change ActionLink to

<li>@Ajax.ActionLink(fileName, "GetContent", new { FileName = fileName }, new AjaxOptions() { UpdateTargetId = "divContent", HttpMethod = "POST" })</li>. 

See http://forums.asp.net/t/1681358.aspx?Disable+cache+in+Ajax+ActionLink+extension+method+in+asp+net+MVC

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