6 Replies - 8069 Views - Last Post: 27 August 2012 - 02:11 PM

#1 mil1234  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 109
  • Joined: 01-February 09

pass varaiable or session data from view to controller

Posted 26 August 2012 - 05:34 AM

Hi Guys,

how should i pass a variable or preferably session data from view to controller?
What i need is to store this
Page.User.Identity.Name
in a string variable or pass it as it is to the below controller so i can use the name passed in my query :
 public IList<Employee> ViewAll()
        {
            var emps = from e in _dataContext.Employees 
                       select e ;
            return emps.ToList();
        }



Thanks..

Is This A Good Question/Topic? 0
  • +

Replies To: pass varaiable or session data from view to controller

#2 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4332
  • View blog
  • Posts: 12,127
  • Joined: 18-April 07

Re: pass varaiable or session data from view to controller

Posted 26 August 2012 - 08:52 AM

Your view should be strongly typed, meaning when you submit data through the page (through such things like a form) it packages up your data into an object and passes it back to the controller, where it comes in as a parameter to the another version controllers method.

Look up the idea of [HttpPost] attributes on a controller's method. Notice this method typically takes an instance of a model class you have defined and which has properties that match that of a form in your view. So for instance if the view had a form on it with a "name" textbox, the model used in this controller method would have a "name" property which is filled with the data the user entered.

This is how you "typically" pass data back to the controller. As you can imagine you can also use [HttpGet].

Now keep in mind that info used in the page should be provided by the controller. If you need the user's name you should be able to just use User.Identity.Name in the controller and bypassing anything that requires the view to pass back info to the controller that is not directly related to the user entering information into the view.

:)
Was This Post Helpful? 1
  • +
  • -

#3 Nakor  Icon User is offline

  • Professional Lurker
  • member icon

Reputation: 444
  • View blog
  • Posts: 1,492
  • Joined: 28-April 09

Re: pass varaiable or session data from view to controller

Posted 26 August 2012 - 06:28 PM

Quote

public IList<Employee> ViewAll()
{
   var emps = from e in _dataContext.Employees 
              select e ;
   return emps.ToList();
}



The view is not where the Page.User.Identity.Name property would be coming from. If the method above is inside of a controller then you already have access to the Page.User.Identity.Name property, there's no need to pass it from anywhere. It is available from within every controller automatically, there's no need to store it in session.

I'm having a hard time trying to put the description of your problem with the code sample you provided. That code is not a controller, a controller is a class not a method. No where in the code you provided are you attempting to use the Page.User.Identity.Name property.
Was This Post Helpful? 1
  • +
  • -

#4 mil1234  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 109
  • Joined: 01-February 09

Re: pass varaiable or session data from view to controller

Posted 26 August 2012 - 10:02 PM

Hi,what i want to do is to add a where clause near _dataContext.Employees and pass it Page.User.Identity.Name, but this is not being identified. The below is a method from EmployeeModel and not controller. You had all reason to be confused by what i ve written!!!
In the controller class 'Page.User.Identity.Name', is recognized, but how can i send it to the model??

public IList<Employee> ViewAll()
{
var emps = from e in _dataContext.Employees
select e ;
return emps.ToList();
}

Thanks...
Was This Post Helpful? 0
  • +
  • -

#5 Nakor  Icon User is offline

  • Professional Lurker
  • member icon

Reputation: 444
  • View blog
  • Posts: 1,492
  • Joined: 28-April 09

Re: pass varaiable or session data from view to controller

Posted 27 August 2012 - 04:08 AM

You need to change your method so that it can accept a parameter. In this case that parameter needs to be a string data type since Page.User.Identity.Name is a string. Then when you call this in the controller you simply pass the Page.User.Identity.Name property in as the parameter. Below is how the updated method might look. You'll need to change the e.Whatever to the actual name of the field you're wanting to compare.

public IList<Employee> ViewAll(string identity)
{
    var emps = from e in _dataContext.Employees
    where e.Whatever == identity
    select e ;

    return emps.ToList();
}


Was This Post Helpful? 1
  • +
  • -

#6 mil1234  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 109
  • Joined: 01-February 09

Re: pass varaiable or session data from view to controller

Posted 27 August 2012 - 05:54 AM

Hello nakor,

thanks for helping me understand that i have to put a parameter in viewAll method which will accept my string. My only problem is now how i would pass the string (in my case - Page.User.Identity.Name) from :

 <telerik:RadMenuItem runat="server" Text="View All Employees" NavigateUrl= "../../Employee/ViewAll"  >
                                </telerik:RadMenuItem> 



Thanks..

This post has been edited by mil1234: 27 August 2012 - 05:59 AM

Was This Post Helpful? 0
  • +
  • -

#7 Nakor  Icon User is offline

  • Professional Lurker
  • member icon

Reputation: 444
  • View blog
  • Posts: 1,492
  • Joined: 28-April 09

Re: pass varaiable or session data from view to controller

Posted 27 August 2012 - 02:11 PM

there is another option for getting the current user instead of having to pass it in as a variable that I didn't think about.

public IList<Employee> ViewAll()
{
    string identity = HttpContext.Current.User.Identity.Name;

    var emps = from e in _dataContext.Employees
    where e.Whatever == identity
    select e ;

    return emps.ToList();
}

I believe you'll need to add "using System.Web;" to the list of using items at the top of that file if it's not already there.


Was This Post Helpful? 1
  • +
  • -

Page 1 of 1