asp.net-mvc是否可以获取ViewData并将其粘贴到javascript函数中

时间:2009-08-04 03:11:22

标签: asp.net-mvc

我有以下javascript从某个地方获取HTML并将其粘贴在textarea中以获取tinymce。

我的问题是如何在asp.net-mvc中获取HTML Here并将其粘贴在javascript中?

我把它放在ViewData中吗?

function ajaxLoad() {
        var ed = tinyMCE.get('elm1');
        // Do you ajax call here, window.setTimeout fakes ajax call
        ed.setProgressState(1); // Show progress
        window.setTimeout(function() {
            ed.setProgressState(0); // Hide progress
            ed.setContent('HTML Here');
        }, 500);
    }

我希望下面有这样的内容,但它似乎不起作用:

function ajaxLoad() {
        var ed = tinyMCE.get('elm1');
        // Do you ajax call here, window.setTimeout fakes ajax call
        ed.setProgressState(1); // Show progress
        window.setTimeout(function() {
            ed.setProgressState(0); // Hide progress
            ed.setContent(<% ViewData["test"] %>);
        }, 500);
    }

3 个答案:

答案 0 :(得分:5)

我认为ajax调用可能最适合你,但也要确保你正在尝试<%= ViewData["test"],请注意第一个百分号之后的“=”。你给出的例子不会像你那样发出ViewData字段的值(也许这只是一个错字?)。

答案 1 :(得分:3)

如果您在aspx或ascx页面中,那么您可以像在您的示例中那样完成 - 只需稍作修改:

ed.setContent(<%= ViewData["test"] %>); // the equals sign

如果你在* .js文件中,那么这将无效,但您可以设置

<input type="hidden" id="myTestData" value='<%=ViewData["test"]%>' />

在aspx,ascx文件中然后通过jQuery获取值:

$("#myTestData").val();

编辑:该死的,我错过了结局%&gt;在<input行。

答案 2 :(得分:0)

我用这个;

在我看来;

$.post("/jQueryTests/jQueryAddMessageComment", { commentText: commentText }, function(newComment) {
            $("#divComments" + id.toString()).html(newComment);
        });

如果我正在返回Json数据;

$.post("/Articles/jQueryDownVoteArticle", { id: id }, function(votes) { document.getElementById("ArticleVotes").innerHTML = votes; }, "json");

在我的控制器中;

return PartialView("commentList", new FormViewModel { LastComment = commentText });

重要的一点是你如何回到视图。你也可以像这样回来;

return Json(new FormViewModel { LastComment = commentText });

请记住,您要替换html的控件需要具有唯一ID。

这是你问的吗?