开发者

EmptyResult jQuery callback issue with $.get

I have some code in a controller (HomeController.cs) that gets called from a $.get method in my view.

View Code

        $(document).ready(function() {
        $.get("/Home/Toolbar", function(result) {
            $("body").prepend(result);
        });
     });

HomeController.cs

    [AcceptVerbs(HttpVerbs.Get)]
    public ActionResult Toolbar()
    {
        if (Request.IsAuthenticated && Roles.IsUserInRole("Agents"))
            return PartialView("toolbar");

        return new EmptyResult();
    }
开发者_如何学运维

My issue here is after the EmptyViewResult is returned to the JS, the code doesn't "post back" to the controller anymore. If I remove the "if" conditional and consisently return the PartialView, everything works correctly.

I would like to only include the "toolbar" partial view in the DOM, when the user is in the "Agents" role.


Since it doesn't look like you're doing much custom logic in your actionresult, why not just conditionally include the PartialView in your site.master (or wherever appropriate)? Since it isn't really dynamic (at least based on what I see in the controller) seems wasteful to open up another HTTP connection and grab it over AJAX.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜