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

  • SEARCH
  • Home
  • 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 664811
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T23:38:25+00:00 2026-05-13T23:38:25+00:00

So, I am in a situation, where I need to display a different view

  • 0

So, I am in a situation, where I need to display a different view based on the “Role” that the authenticated user has.

I’m wondering which approach is best here:

[Authorize(Roles="Admin")]
public ActionResult AdminList(int? divID, int? subDivID) 
{
    var data = GetListItems(divID.Value, subDivID.Value);
    return View(data);
}

[Authorize(Roles = "Consultant")]
public ActionResult ConsultantList(int? divID, int? subDivID)
{
    var data = GetListItems(divID.Value, subDivID.Value);
    return View(data);
}            

or should I do something like this

[Authorize]
public ActionResult List(int? divID, int? subDivID)
{
    var data = GetListItems(divID.Value, subDivID.Value);
    if(HttpContenxt.User.IsInRole("Admin")) 
    { return View("AdminList", data ); }

    if(HttpContenxt.User.IsInRole("Consultant")) 
    { return View("ConsultantList", data ); }

    return View("NotFound");
}
  • 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-05-13T23:38:26+00:00Added an answer on May 13, 2026 at 11:38 pm

    In the case where the action is conceptually the same, but the display is different, I would have one action and return different views depending on your discriminator. I’d go with your second example, slightly modified. Note that there is no need to get the data if the user isn’t in an appropriate role.

    [Authorize] 
    public ActionResult List(int? divID, int? subDivID) 
    { 
        var view = HttpContext.User.IsInRole("Admin")
                       ? "AdminList"
                       : (HttpContext.User.IsInRole("Consultant")
                             ? "ConsultantList"
                             : null);
        if (view == null)
        {
            return View("NotFound");
        }
    
        var data = GetListItems(divID.Value, subDivID.Value); 
    
        return View( view, data );
    }
    

    You realize, of course, that you have the potential for an unhandled exception when you refer to the Value of a potentially null Nullable<int>, correct?

    Also, you could, if doing this frequently, refactor the construction of the view prefix into a common method.

    public string GetRolePrefix()
    {
        return HttpContext.User.IsInRole("Admin")
                       ? "Admin"
                       : (HttpContext.User.IsInRole("Consultant")
                             ? "Consultant"
                             : null);
    }
    

    Then call it as

    ...
    var prefix = GetRolePrefix();
    if (prefix == null)
    {
        return View("NotFound");  // more likely "NotAuthorized" ???
    }
    
    ...get model...
    
    return View( prefix + "List", data );
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

HI, I am in a situation that I need to display notification when call
We have a situation where we need to display data from a database in
Situation: I need to make an imap client (using java mail api) that if,
I have a situation where I need to display a double value rounded to
I'm in a situation where I need to display a tree. However some of
I have a situation where I need to display ItemID and ItemName from Items
I have an application that depends on connection It has many Activities.I need to
I have the need for an activity indicator view in my app when different
I have this situation where I need to display a bunch of files pulled
The situation: I need to convert our current development environment from Windows XP 32-bit to

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.