-1

I am new to ASP.NET MVC. Want to pass common data to all pages in the application. Following Pass data to layout that are common to all pages. Still not able to get the value in layout and in the controller.

When user logged in to system I set flag 'UserhasLoggedIn' and redirect to another controller action.In the layout, I have added checks if flag is set to false then menu items should not be displayed and LoginPage should display. When page is redirected to Home Index it doesn't get 'UserhasLoggedIn' flag.

Layout.cshtml:

@using WebApplication2.Models
@model ViewModelBase
<div class="btn-toolbar" style="background-color:dimgrey; padding-left:35px;padding-top:-10px;">
    @if (null != Model && (bool)Model.IsuserLoggedIn)
    {
        foreach (var menuItem in Model.MenuItems)
        {
            <div class="btn-group">
                <button class="btn btn-primary dropdown-toggle" data-toggle="dropdown">menuItem.MenuName <span class="caret"></span></button>
                <ul class="dropdown-menu">
                    @foreach (var subMenu in menuItem.SubMenu)
                    {
                        <li>@Html.ActionLink((string)subMenu.MenuName, (string)subMenu.ActionName, (string)subMenu.ControllerName)</li>
                    }
                </ul>
            </div>
        }
    }

    <div class="btn-group pull-right" style="padding-right:35px">
        @if (null == Model || !(bool)Model.IsuserLoggedIn)
        {
            <button class="btn btn-primary" onclick="location.href='@Url.Action("Login", "Account")'">Log in</button>
        }
        else
        {
            <button class="btn btn-primary" onclick="location.href='@Url.Action("LogOff", "Account")'">Log out </button>
        }
    </div>
</div>

I have created common view model that can be used.

public abstract class ViewModelBase
{
    public bool IsuserLoggedIn;
}

LoginViewModel:

public class LoginViewModel : ViewModelBase
{
    [Required]
    [Display(Name = "Email")]
    [EmailAddress]
    public string Email { get; set; }
    [Required]
    [DataType(DataType.Password)]
    [Display(Name = "Password")]
    public string Password { get; set; }
    [Display(Name = "Remember me?")]
    public bool RememberMe { get; set; }
}

HomeViewModel:

public class HomeViewModel : ViewModelBase
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
}

AccountController Login Action:

public async Task<ActionResult> Login(LoginViewModel model, string returnUrl)
{
    return RedirectToAction("Index", "Home", new HomeViewModel() { IsuserLoggedIn = true, MenuItems=null });
}

HomeController Index action: Not able to get the IsUserLoggedIn to true and not even in the Layout.

public ActionResult Index(HomeViewModel baseModel)
{
     baseModel.FirstName = "First";
     baseModel.LastName = "Last Name";
     return View(baseModel);
}
Community
  • 1
  • 1
user2323308
  • 759
  • 5
  • 16
  • 34

1 Answers1

0

IsuserLoggedIn is a field, not a property and the DefaultModelBinder cannot set its value (it has no setter) so the property is initialized as false in the Index() method (the default value for bool)

Change the base view model to make it a property

public abstract class ViewModelBase
{
    public bool IsuserLoggedIn { get; set; }
}

Side note: Just use @if (Model != null && Model.IsuserLoggedIn) - there is no need to cast a bool to a bool

  • Initially when page loads value is false. But when Login is successful setting value to True in the Login action of account controller. After setting to true why it is not reflected in the model and in the layout – user2323308 Oct 11 '16 at 10:05
  • Did you even read the answer? The value of `IsuserLoggedIn` in the`Index` method is `false` because the `DefaultModelBinder` cannot **SET** it –  Oct 11 '16 at 10:06
  • Do I need to explicitly set value to true in the Index method?. – user2323308 Oct 11 '16 at 10:08
  • Read the answer!! Make `IsuserLoggedIn` a property by adding `{ get; set; }` –  Oct 11 '16 at 10:08