0
votes

I am developing website in Asp.net core 3.0 razor pages, My website which have a layout page. im storing some data like username, role name in TempData in login page model class like this

     [TempData]
     public int FormRoleId{ get; set; }

     [TempData]
     public int FormUserId { get; set; }

    [TempData]
    public string FormUserName { get; set; }

     public IActionResult OnPost()
    {
        if (string.IsNullOrEmpty(eUserName.Trim()))
        {
            ModelState.AddModelError("UserName", "User Name is a required field.");
            return Page();
        }
        if (string.IsNullOrEmpty(ePassword.Trim()))
        {
            ModelState.AddModelError("Password", "User Name is a required field.");
            return Page();
        }

        var userresult = from usr in _context.TblAllUserDetails
                         where usr.UserName == eUserName.Trim() && usr.Password == ePassword.Trim() && usr.IsDeleted != true
                         select usr;

        if (userresult != null && userresult.ToList().Count > 0)
        {
            int rolid = 0;
            foreach (var item in userresult)
            {
                rolid = item.RoleId;
                FormRoleId = item.RoleId;
                FormUserId = item.UserId;
                FormUserName = item.UserName;
                FormEmailId = item.EmailId;
                TempData.Keep();
            }

            if(rolid == 1)
            {
                return RedirectToPage("./Admin/Dashboard");
            }
            else if (rolid == 2) //Main User
            {
                return RedirectToPage("./Agency/Dashboard");
            }                
        }

How to get these tempdata values in _layout pages in shared folder. im using @Model.FormUserName in _layout page, giving exception like this RuntimeBinderException: 'LEMS_Demo.Pages.Admin.DashboardModel' does not contain a definition for 'FormUserName'

1

1 Answers

1
votes

im using @Model.FormUserName in _layout page, giving exception like this RuntimeBinderException: 'LEMS_Demo.Pages.Admin.DashboardModel' does not contain a definition for 'FormUserName'

Since the DashboardModel doesn't contain the FormUserName property, it will display above exception.

To get the TempData values, you could use the following code:

        @{
            if (TempData["FormUserName"] != null)
            {
                <h3>Message: @TempData["FormUserName"]</h3>
                TempData.Peek("FormUserName");
            }
         }

More detail information about using TempData, please check the following links:

Session and state management in ASP.NET Core

TempData In Razor Pages