I'm working on a simple, custom login system where users can make an account and log in. In this system I'm using Sessions to make sure if user is logged in or not. I'm using MVC4 as well with Entity Framework.

Right now, I want to make a function where users can change their data (name, adress, phone, password). Also, when you log in, you will see your name, last login time and a link to an edit page where you're able to edit your data.

The problem is that I'm not sure how I can retrieve the information/data about the user after they log in. Let me show you some code and explain underway.

This is my UserModel. Since I'm using MVC, I have a controller and View-pages too.

public class User {
    public int ID {get; set;}

    [Required(ErrorMessage = "Specify you email adresse")]
    public string email {get; set;} // this will be the user name

    [Required(ErrorMessage = "Specify your name")]
    public string name {get; set;}

    [Required(ErrorMessage = "Specify your adresse")]
    public string adresse {get; set;}

    [Required(ErrorMessage = "Specify your password")]
    public string password {get; set;}
}

public class dbUser {
    [Key]
    public int ID {get; set;}
    public string email {get; set;}
    public string name {get; set;}
    public string adresse {get; set;}
    public byte[] password {get; set;}
    public Datetime lastLoginTime {get; set;}

}

public class DatabaseContext : DbContext
// Having my Context class with public DbSet<dbUser> Users;

Since I'm using MVC4, I do have a Controller and View-pages. My Controller looks like this (not all of the code, but you'll get the idea):

public class UserController : Controller {

    public ActionResult LogIn() {
        if (Session["LoggedIn"] == null)
            {
                Session["LoggedIn"] = false;
                ViewBag.LoggedIn = false;
            }
            else
            {
                ViewBag.LoggedIn = (bool)Session["LoggedIn"];
            }

            return View();
    }

    public ActionResult LogIn(FormCollection input) {
        string email = input["Email"];
        string password = input["Password"]; // These are from my input fields. 

        if(userOK(email, password)) { // This method checks if the user exits. Won't write the method here. 
            Session["LoggedIn"] = true;
            ViewBag.LoggedIn = true;
            return RedirectToAction("MyPage");
        }
        else {
            Session["LoggedIn"] = false;
            ViewBag.LoggedIn = false;
            return View();
        }
    }

    public ActionResult MyPage {
        if(Session["LoggedIn"].equals(true)) {
            return View();
        }
        return RedirectToAction("LogIn");
    }
}

This is just an illustration of how my Controller looks like. When we log in, we check the session and if the user exits. If it does, it will redirect to MyPage.

And now my problem. When I get to MyPage, I want to be able to give the user a Welcome "YourName" message.
For example *Welcome Bob! Your last login time was 30.10.2013 13:37. *

The thing is that I'm not sure how I can get access to these data from my View. So I can't go to the edit details page.

My MyPage-View looks like this:

@model Project.Models.dbUser
@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>My Page</title>
</head>
<body>
    <div>
        <h2>Welcome @Models.name</h2>
        <p>Your last login time was @Model.lastLoginTime </p>


        @Html.ActionLink("Edit your details", "Edit", new { id = @Model.ID }) // Want to edit my details. 

        <form action="/User/Logout">
            <input type="submit" value="Log out" />
        </form>

    </div>
</body>
</html>

The thing is that none of the @Model.XX works. I get an NullReferenceException error or something. It seems like it doesn't know what @Model.name, @Model.lastLogInTime, @Model.ID is.

Why won't it work? How can I solve this problem?

Also, what's the easiest way to edit the details? Right now my plan was to send the ID as parameter to the Edit-method in controller, find the user and edit data. Easier way to do it?

Thanks in advance.

I think I found a solution. I can simply find the online user and send it as parameter on my View. View(foundUser). what is the simplest way finding a user?

I tried this one, but it didn't work.

 public dbUser findUser(String email)
        {
            using (var db = new ContextClass())
            {
                dbUser user = (from p in db.Users
                                     where p.email.Equals(email)
                                     select
                                         new dbUser()
                                         {
                                             ID = p.ID,
                                             email = p.email,
                                             name = p.name,
                                             adresse = p.adresse,
                                             password = p.password,
                                             lastLoginTime = p.lastLoginTime
                                         }).FirstOrDefault();
                return user;
            }
        }

And on my Usercontroller, MyPage view, I would simply use

if(Session["LoggedIn"].equals(true)) {
    return View(findUser("test@gmail.com"));
}

I think this would solve my problem. However, when I run the program, I get an error: System.NotSupportedException, that my dbUser can't be constructed in a Linq-to Entity query or something..

You don't need to be so heavy handed here.

db.Users.SingleOrDefault(u => u.Email == email); will achieve the same effect. All you need to do then is transfer that data to your view model.

If I were you I'd have some kind of data loader on your VM that you can just pass this object in to.

On a different note, looking at your view, you should have the ID. Performing lookups using this ID will be much faster than a string comparison search.

Also, as strings are nullable, I tend to stay away from using .Equals as this can give you a misleading null reference exception should the object you're calling it on happen to be null.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.