0

I'm looking to see if there is anyway to pass a parameter to the controller itself and not a method inside the controller.

I use named connection strings with parameters for my ADO.NET models. This means I must pass a parameter every time I call the model

MyEntities myentity = new MyEntities(Param);

We do this because each division in our company has it's own mirrored SQL server and when using data we want the local divisions data.

The problem I face is that I cannot put the connection to the Model outside of a Method because it requires a parameter.

What I currently have to do is this

namespace WebApps.Controllers
{
    public class MyController : Controller
    {
            public ActionResult Index(string Param)
            {
                MyEntities myentity = new MyEntities(Param);
                var db = myentity.table;
                return View(db);
            }
    }
}

What I'd like to do is this...

namespace WebApps.Controllers
{
    public class MyController : Controller
    {
        MyEntities myentity = new MyEntities(Param);

            public ActionResult Index()
            {
                var db = myentity.table;
                return View(db);
            }
    }
}

This would also allow me to properly use a dispose method. I can't currently use it because I need to pass that parameter to close the db. (it doesn't exist outside the method)

This fails...

protected override void Dispose(bool disposing)
    {
        if (disposing)
        {
            this.Dispose();
        }
        base.Dispose(disposing);
    }

Could anyone tell me if passing a parameter directly to the controller is possible?

8
  • 1
    From where would it get this Param? I'm not clear what you are trying to accomplish. Commented Sep 3, 2015 at 20:41
  • Not sure if this is a duplicate because it's a bit older and things may have changed, but look at stackoverflow.com/questions/15332159/… Commented Sep 3, 2015 at 20:41
  • @stephen.vakil: My read is that he wants a constructor with a parameter that accepts Param (but my read may be wrong). Commented Sep 3, 2015 at 20:42
  • @EricJ. right, but where is Param instantiated? One alternative is a controller factory... Commented Sep 3, 2015 at 20:44
  • I'm sorry if I'm confusing you. If you look at a default scaffolded controller on an ADO.NET model it automatically puts the Entity connection outside of the ActionResult. So you're able to just call 'myentity' in any method in that controller. I instead have to call the entity inside every method, always passing a parameter from the View to the Method. I wanted to know if I can give the controller a parameter instead of always giving it to the methods... that does sound strange after I type it out... Commented Sep 3, 2015 at 20:48

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.