Daza Aza
Daza Aza

Reputation: 498

Finding CustomerID by matching Customer.UserName to Logged On User

New to MVC3 Razor - linq to sql having spent some time trying to find how to:

Match the loged on UsersName to Customer.UserName and then select the Customer.CustomerID for the user.

public ViewResult Index()
    {
        var CustomerID = from c in Customer
            where c.UserName = "User.Identity.Name"
            select c.CustomerID;

        return View();
    }

Upvotes: 2

Views: 180

Answers (2)

gprasant
gprasant

Reputation: 16023

YOur use of "User.Identity.Name" is wrong. You need to use the Property in HttpContext

In a more concise form, you could say

public ViewResult Index()
{
   var currentUser = Customer.Where(c => c.UserName == HttpContext.User.Identity.Name);
   return View(currentUser);
}

Upvotes: 1

David Hoerster
David Hoerster

Reputation: 28701

Just use the actual variable:

public ActionResult Index()
    {
        var CustomerID = from c in Customer
            where c.UserName == User.Identity.Name  //use double equals for comparison
            select c.CustomerID;

        return View(CustomerID);  //or whatever your view is -- some typed object?
    }

Upvotes: 3

Related Questions