Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8315687
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T21:11:19+00:00 2026-06-08T21:11:19+00:00

I have a custom modelbinder, its check the authentication cookie and return the value.

  • 0

I have a custom modelbinder, its check the authentication cookie and return the value.

public class UserDataModelBinder<T> : IModelBinder
{
    public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
    {

        if (controllerContext.RequestContext.HttpContext.Request.IsAuthenticated)
        {
            var cookie =
                controllerContext.RequestContext.HttpContext.Request.Cookies[FormsAuthentication.FormsCookieName];

            if (cookie == null)
                return null;

            var decrypted = FormsAuthentication.Decrypt(cookie.Value);

            if (!string.IsNullOrWhiteSpace(decrypted.UserData))
                return JsonSerializer.DeserializeFromString<T>(decrypted.UserData);
        }

        return null;
    }
}

if I need to use it, I just need to pass it to the action. everything works.

public ActionResult Index(UserData userData)
{
    AccountLoginWidgetVM model = new AccountLoginWidgetVM();
    if (null != userData)
        model.UserData = userData;

    return View(userData);
}

However, I want to use it in my master page, because once user login, i want to display their info on the top on every page. I tried a few things, coudln’t get it work

 @Html.RenderPartial("LoginPartial", ???model here??)  
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T21:11:20+00:00Added an answer on June 8, 2026 at 9:11 pm

    We did it as follows:

    1. Defined separate viewmodel for masterpages.

      public class MasterPageViewModel
      {
          public Guid CurrentUserId { get; set; }
          public string CurrentUserFullName { get; set; }
      }
      
    2. Added injection filter and filter provider.

      public class MasterPageViewModelInjectorFilterProvider: IFilterProvider
      {
          public IEnumerable<Filter> GetFilters(ControllerContext controllerContext, ActionDescriptor actionDescriptor)
          {
              return new [] {new Filter(new MasterPageViewModelInjectorFilter(), FilterScope.Action, null), };
          }
      
          private class MasterPageViewModelInjectorFilter: IResultFilter
          {
              public void OnResultExecuting(ResultExecutingContext filterContext)
              {
                  var viewResult = filterContext.Result as ViewResult;
                  if (viewResult == null)
                      return;
      
                  if (viewResult.ViewBag.MasterPageViewModel != null)
                      return;
      
                  //setup model whichever way you want
                  var viewModel = new MasterPageViewModel();
                  //inject model into ViewBag
                  viewResult.ViewBag.MasterPageViewModel = viewModel;
              }
      
              public void OnResultExecuted(ResultExecutedContext filterContext)
              {
              }
          }
      }
      
    3. Configure filter provider:

      //in Application_Start
      FilterProviders.Providers.Add(new MasterPageViewModelInjectorFilterProvider());
      
    4. Use in master:

      ViewBag.MasterPageViewModel
      

    This way you have fine uncoupled architecture. Of course you can combine it with Dependency Injection (we do, but I left it out for clarity) and configure your action filter for every action whichever way you want.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have custom validation rule: public function customRule($check) { } Inside this rule I
I have a custom modelbinder built to handle my accountRequest class. This class consists
I'm using ASP.NET MVC 3 and have my custom model binder. public class NewsModelBinder
I have custom class like the below; Public MyClass Public Property MyText() As String
This is my custom model binder. I have my breakpoint set at BindModel but
This is my custom model binder code for the BaseContentObject class: public class BaseContentObjectCommonPropertiesBinder
I want to create a custom modelbinder which validates the bounded model. I have
Suppose I have this model: public class ViewModel { [Required] public string UserInput {
I have a custom model class which contains a decimal member and a view
For a user object in my asp.net mvc project, I have written a custom

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.