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 7959737
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T04:37:11+00:00 2026-06-04T04:37:11+00:00

From a view ‘x’, when the user clicks on ‘Edit Item’, the Controller action

  • 0

From a view ‘x’, when the user clicks on ‘Edit Item’, the Controller action that renders the ‘Edit’ page is also checking for the user’s permissions to that page.

What I’m trying to do is to check for those permissions and if the user does not have them, I would like to display an error message on that ‘X’ view and cancel the controller action.

Right now, my code looks like this:

 [HttpPost]
    public virtual ActionResult EditPage(int? itemId)
    {
        var model = new EditPageModel();
        if (itemId.HasValue)
        {
            var obj = new Item(itemId.Value);

   // Check for user's edit permission before we do anything else.

     var request = SecurityRequest.Create(obj, Item_Edit);
     Request.Execute(() => SecurityManager.ValidatePermissions(request));
            if (!request.IsValid(Item_Edit))
            {
                //skip the rest and return error
                Response.StatusCode = (int)HttpStatusCode.Forbidden;
                // Need Help Here!!!
            }

      // Mode code executes      
      return View(model);
    }
  • 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-04T04:37:12+00:00Added an answer on June 4, 2026 at 4:37 am

    One way you could do this (and this is an example of what I use in a current project) is to create your own ActionFilter. Here’s my example:

    public class UserAuthenticatedAction : FilterAttribute, IAuthorizationFilter
    {
        public void OnAuthorization(AuthorizationContext filterContext)
        {
            if (IsContextFromLoginController(filterContext))
            {
                return;
            }
    
            //...Do whatever you need to check.
    
            if (userNotAllowed){
                SetRedirectToLoginErrorPageForContext(filterContext);
            }
    
            return;
        }
    
        private static void SetRedirectToLoginErrorPageForContext(AuthorizationContext filterContext)
        {
            filterContext.Result = new RedirectToRouteResult(
                new RouteValueDictionary
                    {
                        { "controller", "Login" },
                        { "action", "LoginError" },
                        { "targeturl" , filterContext.RequestContext.HttpContext.Request.Url.ToString()} }
                    });
        }
    
        private static bool IsContextFromLoginController(AuthorizationContext filterContext)
        {
            var controllerName = GetCurrentControllerName(filterContext);
    
            return controllerName.Equals("Login");
        }
    
        private static string GetCurrentControllerName(AuthorizationContext filterContext)
        {
            return filterContext.ActionDescriptor.ControllerDescriptor.ControllerName;
        }
    }
    

    To use this filter, in the global.asax.cs file, I have this line in Application_Start():

    GlobalFilters.Filters.Add(new UserAuthenticatedAction(excludeActions, allowedRoles));

    That means the attribute is executed whenever an ActionMethod is called. Basically it checks if the user is allowed to access a specific url and if that user is not allowed, then the filterContext is set to redirect them back to the login page.

    I also check whether the current url is already the login page; if that’s the case then don’t bother doing the security check.

    I guess there are more fancy ways of doing this, depending on your requirement, but the above method works fine for our needs.

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

Sidebar

Related Questions

Can I call javascript function from MVC controller action (not from view page) and
I have to pass values from view to controller, controller having action method that
My Controller Action accepts int id. What to do when calling from View ActionLink
I would like to pass parameter from view to controller in cshtml page. This
I'd like my controller to subscribe to notifications from view. However, before doing that,
When submitting one form from view, how can i also read or have controller
I'm trying to send the user id (integer) from a view to the action.
I wanted to call controller action or controller function from view file. but i
I want to pass values(inputs from user)from view controller class to objective c class
How to call codeigniter controller function from view? When i call the function in

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.