-3

I am new in .net mvc, I am working on Visual Studio 2012. I just want to fetch data from database's table in controller and store this value into different variable. For example:

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

Now I just want to store this model value into different variable such as string, int etc depend on the necessary.

Fedor
  • 1,548
  • 3
  • 28
  • 38
fahreyad
  • 11
  • 1
  • 2
    you need to add more context.what are you trying to do? why do you need a global variable – Prabhu Murthy Feb 17 '14 at 12:37
  • yes, I want to set this LINQ result in global variable as well as Session variable – fahreyad Feb 17 '14 at 12:39
  • So, what's problem do you experience with it? – Fedor Feb 17 '14 at 12:40
  • 2
    Perhaps an [Entity Framework Quickstart Tutorial](http://msdn.microsoft.com/en-us/library/vstudio/bb399182(v=vs.100).aspx) or an [ADO.Net Tutorial](http://msdn.microsoft.com/en-us/library/e80y5yhx(v=vs.110).aspx) might help. – MattC Feb 17 '14 at 12:41
  • I am trying to set user IP address which must be matched from database and use this IP address in the whole application – fahreyad Feb 17 '14 at 12:42
  • Take a look [at this SO answer](http://stackoverflow.com/a/7541043/1177964), if you want an example of how to store variable in `Session` object – Fedor Feb 17 '14 at 12:47
  • 1
    What is your problem exactly? is it how to store data in `session`? or how to fetch data from `database` by `LINQ`? – Mohamed Salah Feb 17 '14 at 13:20

1 Answers1

0

First you have to learn more about mvc 4. There are lots of tutorial in internet. check this out

Here are some sample code ::

In your Controller :

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

In you View (cshtml) ::

@{
    var someData = ViewBag.someData; // now you can use this data in your cshtml file
}
sabbir
  • 2,020
  • 3
  • 26
  • 48