如何显示登录用户的具体记录?(MVC4)

时间:2016-10-20 14:28:02

标签: asp.net-mvc-4

我希望创建一个配置文件页面,它将显示用户个人记录,并使用ASP.NET MVC 4> Internet应用程序。例如,我有一个表UserProfile

UserID   UserName   Name   Age   Gender                                             

1          john     John    17     Male

2          dave     Dave    15     Male

3          mark     Mark    20     Male

然后如果我使用UserName john登录,则用户名john的所有信息将仅显示在个人资料页面中

*抱歉,我觉得很难解释

public class HomeController : Controller     //HomeController
{
    public ActionResult Index()
    {
        ViewBag.Message = "Modify this template to jump-start your ASP.NET MVC application.";

        return View();
    }

    public ActionResult About()
    {
        ViewBag.Message = "Your app description page.";

        return View();
    }
public ActionResult Contact()
    {

        return View();
    }
}






public class UsersContext : DbContext    // AccountModel
{
    public UsersContext()
        : base("DefaultConnection")
    {
    }

    public DbSet<UserProfile> UserProfiles { get; set; }
}

[Table("UserProfile")]
public class UserProfile
{
    [Key]
    [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
    public int UserId { get; set; }
    public string UserName { get; set; }
}

1 个答案:

答案 0 :(得分:0)

获取您的用户名

 string username = FormsAuthentication.Decrypt(Request.Cookies[FormsAuthentication.FormsCookieName].Value).Name;

然后

var record=from c in db.tablename where UserName   = username select c