开发者

Pass property from controller to Model

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 fa开发者_运维问答r 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!


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);
  }


Try using session variable.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜