1

Login 的项目也会检查用户的详细信息和他们所属的服务区域。这只是一个领域,但现在它们可以属于多个领域。

该代码是如何设置为使用Session.

var user = (from u in db.tbl_UserServiceAreaDetails
            where u.tbl_User.UserName.Equals(txt_LoginName.Text)
            && u.tbl_User.Password.Equals(txt_Password.Text)
            select u).FirstOrDefault();

if (user != null)
{
Session["Username"] = user.tbl_User.UserName;

Session["ServiceArea"] = user.tbl_ServiceArea.ServiceArea;

Session["ServiceAreaID"] = user.serviceAreaID;

现在ServiceArea可以很多。我可以尝试foreach在和serviceAreaID之间的链接表中的UserServicearea被映射到in并映射到in 。serviceAreaDetailsserviceAreaIDserviceAreaIdtbl_ServiceAreaUserAreaIDuserIdtbl_User

foreach (ListItem item in user.ServiceAreaID)能工作吗?

如果我得到userId用户登录的时间并将其传递给我,我Foreach可以将其设置为Session. 由于索引页面显示servicearea用户也属于,现在可以不止一个。

4

2 回答 2

1

您也可以在会话中存储复杂的对象,因此为您的用户创建一个类,其中所有具有 id 的区域都存储给用户并将该类保存在会话中

class UserClass
{
    public string userName;
    public Dictionary<int, string> serviceArea = new Dictionary<int,string>();
}

UserClass user = new UserClass();

Session["User"] = user;

user = (UserClass)Session["User"];

回复评论:
是的,随意使用Getter&Setter
是的,字典就是这样工作的,第一个参数是键,第二个是值,它也可以灵活地与任何对象一起使用

UserClass user = new UserClass();

user.userName = "Rik";

foreach(...serviceArea...) // Or whatever loop you like
{
    user.serviceArea.Add(serviceAreaID, serviceArea);
}
于 2013-03-06T12:03:59.080 回答
0

我发现有用的是创建一个对象列表并将其添加到会话中。然后,您可以将会话变量转换为该对象类型并对其进行迭代。例如:

Public Class someClass
    Public Property userName as String
    Public Property FirstName as String     
End Class

然后,您可以创建此类型的列表并将其添加到会话中:

dim myList as new list(of someClass)
dim newSomeClass as new someClass

newSomeClass.userName="username1"
newSomeClass.FirstName="Jason"
myList.add(newSomeClass)

newSomeClass.userName="username2"
newSomeClass.FirstName="Frank"
myList.add(newSomeClass)

Session.add("mySessionId",myList) 

然后,您可以将该会话转换为 myList 并在其他地方对其进行迭代:

Dim myList as new list(of someClass)
if(Session("mySessionId") isNot nothing) then
    myList = Session("mySessionId")

    for each n as someClass in myList
      'iterate
    next
end if

You just need to be careful that you destroy these session when finished and that you aren't consuming to much resources between postbacks. If you have any questions, let me know.

于 2013-03-06T12:11:17.190 回答