Tuesday, January 8, 2013

How to scroll a large text that doesn't scroll?

How to scroll a large text that doesn't scroll?

Just add this css element:

overflow: visible;

Monday, January 7, 2013

Need a Logout link on every page of your MVC 3 web site?

Need a Logout link on every page of your MVC 3 web site? This is fairly easy to do:

First thing to do is add a new Logout member of your Controller class as:


public PartialViewResult Logout(){  AdminData data = new AdminData( AccountController.OnlineUsers.Count );  return PartialView( data );}

What is AdminData? Just a small class that holds a number:

public class AdminData{
  public decimal LoggedInUsers { get; set; }
  public AdminData( decimal loggedInUsers )
  {
    LoggedInUsers = loggedInUsers;
  }
}

What should be in the view? Just this:

@model My_MSI.Net.Areas.Admin.Models.Entities.AdminData
 @using ( Ajax.BeginForm( "OnlineUsers", "BackOffice", new { area = "" }, new AjaxOptions { UpdateTargetId = "onlineUsers", HttpMethod = "Post" } ) )
{
  <table width="95%">
    <tr>
      <td style="text-align: right; width: 90%;">
       <span style="text-align: right;">We currently have
                       AjaxExtensions.ActionLink(  Ajax, @Model.LoggedInUsers.ToString),
                                                                      "OnlineUsers", "BackOffice",
                                                                       new { area = "" },
                                      new AjaxOptions { UpdateTargetId = "onlineUsers", HttpMethod = "Post" } )
                                      user(s) logged in.</span>
      </td>
      <td style="text-align: right; width: 10%;">
         @Html.ActionLink( Log Out", "LogOff", "BackOffice", new { area = "" }, null )
      </td>
    </tr>
  </table>}

This view may be more complicated than you want. Let me explain what it is doing:

The <td> with a <span ....>...</span> in it is actually an Unobtrusive AJAX link as the number of users currently logged into the BackOffice (password protected part of the site).

We put the LogOff member in a base class that all controllers derrive from and it looks like this:

public RedirectToRouteResult LogOff()
{
  if ( AccountController.OnlineUsers.Count >= 1 )
  {    LoggedInUsers member = AccountController.OnlineUsers
                                   .First( m => m.MemberName == User.Identity.Name );
       if ( member != null )
          AccountController.OnlineUsers.Remove( member );
  }
  FormsAuthentication.SignOut();
  return RedirectToAction( "Index", "Home",
                            new { MemberName = User.Identity.Name, area = "" } );
}

This processing takes place within the if (...) { } block:
We check the number of OnlineUsers in the first if( AcountController.OnlineUsers.Count >= 1 ) statement because if this list is empty we don't need to remove anyone.

Next we find the LoggedInUsers record for the current User.Identity.Name because this is the logged in member name.

Next we verify that the member is not null because that can happen sometimes.

If and only if the member is not null, we remove that member from the AccountController.OnlineUsers list.

Now we are outside the if() { } block:

The first thing that you'll want to do is do a: FormsAuthentication.SingOut(); This signs out the user and removes that authentication cookie from the HTTP stream.

Finally we need to RediredtToAction to the publicly accessable portion of the site.

That's almost everything that you need to do to get a Logout link on every page of your site:

The final step is:

In your _Layout.cshtml file,

Find this:
<div class="content">
@RenderBody()
</div>

You want to add this above the @RenderBody() method:

@{ Html.RenderAction( "Logout", "BackOffice" ); }

That's everything you need to do to get a Logout link on every page of your site.