将属性从控制器传递到模型

发布于 2024-11-14 03:16:47 字数 1393 浏览 0 评论 0原文

我正在尝试将变量从控制器中的方法传递到模型中的方法。由于模型中的方法采用一个参数(这是之前设计的),因此我无法将变量作为参数传递给模型中的方法。而且,这个模型中的方法也被其他控制器调用,所以如果我更改参数,我也必须更改所有控制器,这将是一项繁琐的任务。 到目前为止我一直在尝试的是 - 我创建了一个 MyVariableClass 并声明了一个属性。然后我实例化该类并将属性字符串设置为我想要传递的变量。现在,在我的 Model 方法中,我再次实例化了相同的 MyVariableClass,但是当我这样做时,变量的值被设置为 null。我现在拥有的代码是 -

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

以及 MyVariableClass

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

和我的模型中的方法

  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 ....
   }

有人能解决这个问题吗?提前致谢!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

孤单情人 2024-11-21 03:16:47

为什么子类化模型并重写 GetItemInformation 方法不起作用?或者,更简单的是,为什么不直接使用带有两个字符串的方法来重载 GetItemInformation 方法呢?您的其他控制器仍然可以使用仅采用单个字符串的控制器。

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

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);
  }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文