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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T21:15:59+00:00 2026-05-22T21:15:59+00:00

I am trying to pass a variable from a method in my Controller to

  • 0

I am trying to pass a variable from a method in my Controller to a method in a Model. Since the method in the Model takes one argument (which was designed earlier), I cannot pass my variable as an argument to the method in the Model. And also, the method in this Model is called by other controllers too, so if I change the argument, I have to change all the controllers too, which would be a tedious task.
What I have been trying so far is- I created one MyVariableClass and declared a property. Then I instantiated that class and set the property string to the variable that I wanted to pass. Now, in my Model’s method, I instantiated the same MyVariableClass again, but when I did that, the value of the variable was set to null. The code I have right now is –

 public ActionResult ItemInformation( string id)
     {
        //Pass a string to MyVariable
        MyVariableVClass params = new MyVariableClass();
        params.myVariable = "abc";

   //This is what My Model is taking as an argument(id), and I don't want to 
   //pass mYvariable along with that argument because it will break other controllers
  // too which calls this method
    var itemInformation = _repository.GetItemInformation(id);
   return View(itemInformation);
    }

and MyVariableClass

  public  class MyVariableClass
  {
     public string myVariable { get; set; }
  }

and the method in My Model

  public IList<Items> GetItemInformation(string itemId)
    {

      MyVariableClass webType = new MyVariableClass();
      var _params = webType.myVariable;
       //Check this variable and perform database query 
      if (_params =="this") 
       {
        var query = myFirstQuery;
       }
      else
       {
       var query = mySecondQuery;
       }
     //return ....
   }

Anybody has solution to this? Thanks in Advance!

  • 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-22T21:16:00+00:00Added an answer on May 22, 2026 at 9:16 pm

    Any reason why subclassing your model and overriding the GetItemInformation method wouldn’t work? Or, even easier, why not just overload the GetItemInformation method with one that takes two strings? Your other controllers can still use the one that only takes a single string.

      public IList<Items> GetItemInformation(string itemId, MyVariableClass webType)
        {
    
          var _params = webType.myVariable;
           //Check this variable and perform database query 
          if (_params == "this") 
           {
            var query = myFirstQuery;
           }
          else
           {
           var query = mySecondQuery;
           }
         //return ....
       }
    
      public IList<Items> GetItemInformation(string itemId)
      {
         MyVariableClass fauxType = new MyVariableClass();
         fauxType.myVariable = "not this";
    
         return GetItemInformation(itemId, fauxType);
      }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I was trying to pass a variable of type NSString from a method to
I am trying to pass one variable to a jQuery function inline (i.e.: using
I am trying to pass a string that already contains quotation marks from one
I'm trying to pass a string variable from the App Delegate into the a
Hi am trying to pass variables to php from inside an app. <?php //
I'm trying to pass a variable through a ssh connection, like this: working_dir=/home/user/some_dir/ ssh
I am trying to pass a variable to an ajax request via jquery. I
I am trying to pass a variable PIN2 that I am retrieving like below
i am trying to pass in a selector into the ID variable but this
We are trying to create a web-service that we plan to pass a variable

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.