开发者

EmptyResult jQuery callback issue with $.get

开发者 https://www.devze.com 2022-12-17 05:30 出处:网络
I have some code in a controller (HomeController.cs) that gets called from a $.get method in my view.

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

精彩评论

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