开发者

tinymce in mvc 3 razor, Ajax.ActionLinks fail after first ajax call

开发者 https://www.devze.com 2023-04-01 07:32 出处:网络
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

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 &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>

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());
                }

                pub开发者_运维问答lic 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();
                }
            }
        }


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

0

精彩评论

暂无评论...
验证码 换一张
取 消