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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T20:02:54+00:00 2026-06-10T20:02:54+00:00

I know there are lots of delegate/func examples but I can’t find any examples

  • 0

I know there are lots of delegate/func examples but I can’t find any examples that will work for me, or I just don’t understand them.

I’m using asp.net MVC for a website, and the website needs some web service calls for an outside application to interact with my app. These all need a function to execute (going to db and whatnot), and return a similar data model every time. I want to wrap each call in a try/catch and populate the model.

Here is the generic code that happens every call.

var model = new ResponseDataModel();
try
{
     //execute different code here
}
catch (Exception ex)
{
    model.Error = true;
    model.Message = ex.ToString();
}
return View(model); // will return JSON or XML depending on what the caller specifies

This is one of the controller methods/ functions that I am using

public ActionResult MillRequestCoil()
{
    var model = new ResponseDataModel();
    try
    {
        /* edit */
        //specific code
        string coilId = "CC12345";

        //additional code
        model.Data = dataRepository.doSomethingToCoil(coilId);

        //replaced code
        //model.Data = new { Coil = coilId, M3 = "m3 message", M5 = "m5 message" };
        model.Message = string.Format("Coil {0} sent successfully", coilId);
    }
    catch (Exception ex)
    {
         model.Error = true;
         model.Message = ex.ToString();
    }
    return View(model);
}

I would like to be able to somehow convert the specific function to a variable to pass into the generic code. I’ve looked at delegates and anonymous funcs but it’s pretty confusing until you do it yourself.

  • 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-10T20:02:56+00:00Added an answer on June 10, 2026 at 8:02 pm

    Put the following somewhere accessible:

    public static ActionResult SafeViewFromModel(
        Action<ResponseDataModel> setUpModel)
    {
        var model = new ResponseDataModel();
        try
        {
            setUpModel(model);
        }
        catch (Exception ex)
        {
            model.Error = true;
            model.Message = ex.ToString();
        }
        return View(model); 
    }
    

    and then call it like:

    public ActionResult MillRequestCoil()
    {
        return MyHelperClass.SafeViewFromModel(model =>
        {
            string coilId = "CC12345";
            model.Data = new {
                Coil = coilId,
                M3 = "m3 message",
                M5 = "m5 message" };
            model.Message = string.Format("Coil {0} sent successfully", coilId);
        });
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know that there are lots of examples out there on this, but they
I know there are lots of tools on the net that can make our
I know there is a lot on this topic but I can't get any
I know there are lots of FF/Chrome CSS questions, but I can't seem to
I know there are lots of questions with this title but I could find
I know there are lots of truncate scripts out there, but I can't use
I know there's some of those questions on SO already, but I can't find
1) I know there are lots of web sites that describe in what order
I know that there is an allocator for user applications than handles lots of
I know that there are lot's of questons on this, but all seem 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.