Jon Scalet
Jon Scalet

Reputation: 207

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.

Upvotes: 1

Views: 249

Answers (1)

Parrots
Parrots

Reputation: 26892

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.

Upvotes: 2

Related Questions