1

I am working just one week with ASP.NET MVC 3. This might be a very basic question or someone might have asked similar question before. I am seeking help to show me right path/method to accomplish the problem I am facing getting the partial view designed.

I have created a partial view login (in side bar) as below. What I would like to achieve here is, when I hit the "login" button, I must be a able update the partial view with information such as

**Welcome [UserName]

Last SuccessFul Login : [DateTime]

Member Since : [Date]**

I am not sure if I need to create another partial view to display this information Or the login partial view can be updated on the fly based on the action.

Login Partial View

@model AlanBeezLab.Models.LoginModel
@using (Ajax.BeginForm("Login","UserLogin", new AjaxOptions { UpdateTargetId =Model.UserName }))
{
    <div >User Name
    @Html.TextBox(" ")
    </div>
    <div>Password
    @Html.Password(" ")
    </div>
    <p><input type="submit" value="Let me in!" /></p>

}

Below is the _Layout.cshtml

<!DOCTYPE html>
<html>
<head>
    <title>@ViewBag.Title</title>
       <!--link href="@Url.Content("~/Content/Site.css")" rel="stylesheet"    type="text/css" />-->
      <link href="@Url.Content("~/Content/SiteStyle.css")" rel="stylesheet" type="text/css" />
    <script src="@Url.Content("~/Scripts/jquery-1.5.1.min.js")" type="text/javascript"></script>
    <script src="@Url.Content("~/Scripts/modernizr-1.7.min.js")" type="text/javascript"></script>
</head>

<body>
   <div id="Header" style="background-image: url('/Content/Images/Banner_Final3.png'); background-repeat:no-repeat; width :1500px; height : 150px;"   >

   </div>
  <div id="SideBar">
       @Html.Partial("UserControls/UserLogin", new AlanBeezLab.Models.LoginModel())
   </div>
   <div id="Content">
        @RenderBody()
   </div>
   <div id="Footer">
        <p>Copyright &copy; XXXXX</p>
   </div>
</body>
</html>

Below is the Login Controller

public class UserLoginController : Controller
{
    //
    // GET: /UserLogin/

    public ActionResult Index()
    {
        return View();
    }

    public ActionResult LogIn()
    {
        return [Not Sure];
    }

I am not sure if this is best/right approach to accomplish this. I would appreciate if I am directed to the right path/approach.

Thanks

1 Answer 1

0

I've had this discussion with my peers before.

IMO login/logout should not be done via AJAX, it should be a regular HTTP POST.

Why? Well what if your on a page that requires Administrator access, then you logout via AJAX? Your still on the page - when you shouldn't be authorized to do so (unless of course you update the main portion of the page as well, which is kind of silly)

So my advice - make the login button do a regular POST to an action method, then use the PRG pattern to redirect back to the original page.

Of course, if you don't have the concept of administrators/permissions/roles then an AJAX login/logout will be fine.

Regardless, you've done the right thing with the partial view.

Your partial view should figure out if the user is authenticated, then render an appropriate editor/display template based on that.

E.g: _Login.cshtml:

@if(Request.IsAuthenticated) {
   @Html.DisplayFor(Context.User.Identity)
} else {
   @* editor fields *@
}
1
  • I do not have any plan to use Administrator role at this point of time. The above should work for me. Thanks a lot.
    – Alan B
    Jun 10, 2011 at 6:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.