简体   繁体   中英

return partial view as string

I'm trying to reduce the number of get requests I make to the server. I would like to return the partial view to my $.get method as a string so that I can return 3 of them at once instead of having 3 seperate server callbacks. is there anyway to do this. so far I have

so far it just returns 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");
    }

The Method "PartialView" returns the type "System.Web.Mvc.PartialViewResult" so when you try to concatenate the return result of that method it will call ToString of object, which if not overrided simply returns the types name.

So this approach will not work for that reason. What you want to do is mark your actions return type as PartialViewResult.

Then define a partial view that simply writes out the three partial views that you want returned.

If you define a new partial like this:

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

And return that partial, you should get what you're after.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM