2

I have the following partial view:

<span>Login Status</span>
@{
    if (ViewBag.UserId > 0)
    {
        @Html.ActionLink("Log Out", "LogOut", "Home", null);
    }
    else {
        @Html.ActionLink("Log In", "Login", "Home", null);
    }
}

It's just a pretend login status area. Now, the idea is to have this appear in master page for my MVC site, so that it appears on all pages. Here's how I'm rendering it in the _Layout.cshtml file:

<div id="login">
   @{ Html.RenderPartial("LoginStatus"); }
</div>

What I'd like to have is, when the user clicks either the "Log In" or "Log Out" links, an action is performed (to log the user in/out) and then the partial view is refreshed - but the the rest of the page is left alone.

At the moment, when I click any of the links, the site navigates to "Home/Login" or "Home/Logout". I don't want it to render a view, I'd like to refresh just the partial view, regardless of which page I'm on.

What is the suggested way to do this?

Cheers. Jas.

Jason Evans
  • 28,906
  • 14
  • 90
  • 154

2 Answers2

1

You could use AJAX. So you could use jquery to unobtrusively AJAXify those links by subscribing for their click event and sending an AJAX request to the corresponding controller action which would return the partial view and update the DOM.

Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • Thanks for that advice. I've just had a quick go at using Ajax.BeginForm() for this scenario and I'm almost there. I will edit my question with the code I have, but when I submit my partialview, instead of updating a div in the `BeginForm` area, my code navigates to the Home/ that I declared in the `BeginForm`. I'll post the code when I get time, it will make more sense. – Jason Evans Feb 10 '11 at 09:08
0

Try the @Ajax.ActionLink() method.

marcind
  • 52,944
  • 13
  • 125
  • 111