0

我正在尝试减少对服务器发出的获取请求的数量。我想将部分视图$.get作为字符串返回给我的方法,这样我就可以一次返回其中的 3 个,而不是有 3 个单独的服务器回调。有没有办法做到这一点。到目前为止我有

到目前为止它只返回 System.Web.Mvc.PartialViewResult

function updateChat() {

/** I want to make this work so I only have 1 callback */
$.get("Home/refreshChatParts", {}, function (response) {
    var html = response.split(",");
    $("#chatWindow").replaceWith(html[0]);
    $("#chatHeader").replaceWith(html[1]);
    $("#playerList").replaceWith(html[2]);
});

/**  this stuff works, but is a lot of call backs */
$.get("Home/refreshChatText", {}, function (html) {
    $("#chatWindow").replaceWith(html);
});

$.get("Home/refreshChatHeader", {}, function (html) {
    $("#chatHeader").replaceWith(html);
});

$.get("Home/refreshPlayerList", {}, function (html) {
    $("#playerList").replaceWith(html);
});
*/
$.get("Home/getRequest", {}, function (requestUser) {
    if (requestUser.toString() != "failed") {
        pause = true;
        var join = confirm(requestUser + " would like to play a game with you");
        if (join) {
            startGame(requestUser);
        } else
            pause = false;
        resetRequestUser(requestUser);
    }
});

//$.get("Home/checkForGameStart", {}, function (response) { /*alert(response);*/ });

scrollChatDown();

setTimeout('updateChat()', 20000);
}




 public String refreshChatParts() {

        //refresh chat text
        ViewData["ChatText"] = getFormatedChatText();

        //refresh usercount
        try {
            userCount = getUserCount();
            ViewData["numberOfUsers"] = "There are " + userCount + " players online";
        } catch (Exception e) { ViewData["numberOfUsers"] = e.Message; }

        //refresh player list
        ViewData["listOfPlayers"] = getFormatedPlayerList();

        checkForGameStart();

        return PartialView("ChatText") + "," + PartialView("ChatHeader") + "," + PartialView("playerList");
    }
4

1 回答 1

0

方法“PartialView”返回类型“System.Web.Mvc.PartialViewResult”,因此当您尝试连接该方法的返回结果时,它将调用对象的 ToString,如果未重写,则仅返回类型名称。

因此,由于这个原因,这种方法将不起作用。您要做的是将您的操作返回类型标记为 PartialViewResult。

然后定义一个局部视图,简单地写出您想要返回的三个局部视图。

如果你像这样定义一个新的部分:

@{ Html.RenderPartial("ChatText"); } ,
@{ Html.RenderPartial("ChatHeader"); } ,
@{ Html.RenderPartial("playerList"); } 

并返回那个部分,你应该得到你所追求的。

于 2012-05-27T04:01:41.127 回答