从数据库设置全局变量

时间:2014-02-17 12:34:37

标签: c# asp.net-mvc-4

我是.net mvc的新手,我正在使用Visual Studio 2012.我只想从控制器中的数据库表中获取数据并将此值存储到不同的变量中。例如:

public class TestController
{
   public ActionResult TestMethod()
   {
      var model = some LINQ code;
   }
}

现在我只想将此model值存储到不同的变量中,例如stringint等,具体取决于必要的。

1 个答案:

答案 0 :(得分:0)

首先,您必须了解有关 mvc 4 的更多信息。互联网上有很多教程。 check this out

以下是一些示例代码::

在您的控制器中:

 public class testController
 {       
      public ActionResult testMethod()
      {
          ViewBag.someData = // get data from database using Linq or SQL Query
          // or you can use Model/ModelView
          return View(model)
      }    
 }

在你的视图(cshtml)::

@{
    var someData = ViewBag.someData; // now you can use this data in your cshtml file
}
相关问题