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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T22:26:31+00:00 2026-05-12T22:26:31+00:00

I have the following code repeated several times in a mvc app. public ActionResult

  • 0

I have the following code repeated several times in a mvc app.

    public ActionResult AnAction(int Id)
    {           
        var claim = GetClaim(Id);
        if (claim == null)
        {
            return View("ClaimNotFound");
        }

        // do stuff here
        ....
        return ....;
    }

So far this pattern is used 4 times and it’s getting ugly. What is the best way to refactor it?

Edit:
A couple of example usages

    public ActionResult Claim(int Id)
    {           
        var claim = GetClaim(Id);
        if (claim == null)
        {
            return View("ClaimNotFound");
        }

        return View("Claim", claim);
    }

    public ActionResult MedicalPV(int Id)
    {
        var claim = GetClaim(Id);
        if (claim == null)
        {
            return View("ClaimNotFound");
        }

        return PartialView(claim.MedCerts.AsQueryable<MedCert>());
    }

Typically I am requiring access to the object in the view. This particular code is only used in one controller, but I am may need something similar in other controllers with different objects and views.

  • 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-12T22:26:31+00:00Added an answer on May 12, 2026 at 10:26 pm

    If all of the actions require a claim, then you could try to retrieve it in OnActionExecuting and set the Result to the ViewResult on failure. If only some actions, require it, perhaps, an ActionFilter that checks to make sure that a claim is available before executing the method and, if not, sets the proper View.

    private Claim Claim { get; set; }
    
    public override void OnActionExecuting( ActionExecutingContext context )
    {
        this.Claim = GetClaim( int.Parse( context.RouteData["id"] ) );
        if (this.Claim == null)
        {
            context.Result = View( "ClaimNotFound" );
        }
    }
    

    OR

    public class RequiresClaimIdAttribute : ActionFilterAttribute
    {
        public override void OnActionExecuting( ActionExecutingContext context )
        {
             var claim = GetClaim( int.Parse( context.RouteData["id"] ) );
             if (claim == null)
             {
                  context.Result = new ViewResult
                  {
                       ViewName = "ClaimNotFound",
                       ViewData = context.Controller.ViewData
                  };
             }
             else
             {
    
                  var property = context.Controller
                                        .GetType()
                                        .GetProperty( "Claim",
                                                       BindingFlags.Public
                                                       | BindingFlags.NonPublic
                                                       | BindingFlags.Instance);
                  if (property != null)
                  {
                       property.SetValue(context.Controller,claim);
                  }
             }
        }
    }
    
    [RequiresClaimId]
    public ActionResult AnAction( int id )
    {
        this.Claim.Updated = DateTime.Now;
        ...
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following piece of code that is repeated in several files: <tr>
I have the following XML code. You will notice the tag Description is repeated,
I have following code for updating user's column public void UpdateLastModifiedDate(string username) { using
I have the following statments in my source code int tableField1; int tableField2; int
Let's say I have following code: int f() { int foo = 0; int
I have the following code, repeated on each Form , as part of the
I have a quick question, suppose I have the following code and it's repeated
I have the following code: delegate int doStuffDel(int instanceNo, int sleepTime, int repeatCount); string
I do have the following code for taking random int numbers for (int i=1;i<=5;i++)
Morning stackoverflow, I have a repeater, with the following code in my aspx page;

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.