Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/visual-studio-2012/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net mvc 4 ASP.NET MVC 4如何在控制器内管理用户ID和用户内容_Asp.net Mvc 4_Httpcontext_User Management_Simplemembership - Fatal编程技术网

Asp.net mvc 4 ASP.NET MVC 4如何在控制器内管理用户ID和用户内容

Asp.net mvc 4 ASP.NET MVC 4如何在控制器内管理用户ID和用户内容,asp.net-mvc-4,httpcontext,user-management,simplemembership,Asp.net Mvc 4,Httpcontext,User Management,Simplemembership,为了学习ASP.NETMVC4模式,我正在开发一个必须管理一些数据和用户内容的应用程序 这是数据库: public DbSet<UserProfile> UserProfiles { get; set; } public DbSet<Fund> Funds { get; set; } public DbSet<Source> Sources { get; set; } public DbSet<Portfolio> Portfolios { get

为了学习ASP.NETMVC4模式,我正在开发一个必须管理一些数据和用户内容的应用程序

这是数据库:

public DbSet<UserProfile> UserProfiles { get; set; }
public DbSet<Fund> Funds { get; set; }
public DbSet<Source> Sources { get; set; }
public DbSet<Portfolio> Portfolios { get; set; }
public DbSet<Quote> Quotes { get; set; }
public DbSet<Deposit> Deposits { get; set; }
投资组合模型

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

    [Display(Name = "Label")]
    [MaxLength(30)]
    [Required(ErrorMessage = "Insert a label.")]
    public string Label { get; set; }

    [DataType(DataType.Date)]
    [Display(Name = "Created on")]
    [DisplayFormat(DataFormatString = "{0:d}", ApplyFormatInEditMode = true)]
    public DateTime CreationDate { get; set; }

    public virtual int UserId { get; set; } // Foreign key for UserProfile table

    public virtual ICollection<Fund> Funds { get; set; } // Linked Funds
}
那么,这种方法呢?我已经知道这可能不是最好的解决方案

然后,在创建操作中我需要UserId将新的公文包与正确的用户关联起来,那么我要做什么呢

//
// GET: /Portfolio/Create

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

//
// POST: /Portfolio/Create

[HttpPost]
public ActionResult Create(Portfolio portfolio)
{
    if (ModelState.IsValid)
    {
        // TODO:
        // Here I need the UserId to associate the new portfolio with the user!
        // I don't want to make another LINQ query to get the UserId from Username.
        // ...

        _db.Portfolios.Add(portfolio);
        _db.SaveChanges();
        return RedirectToAction("Index");
    }

    return View(portfolio);
}
当然,我需要对存款模型做同样的事情,因为用户创建了一个新的存款,并且只需要查看/编辑他自己的存款

我想知道的是:


在许多控制器操作和视图中,管理用户内容和用户名/用户ID需求的最佳模式(最正确/最优雅)是什么?

最好是使用:
WebSecurity.CurrentUserId
抱歉,我编辑了帖子,而不是添加评论,现在我无法恢复编辑谢谢!我还可以使用
WebSecurity.CurrentUserName
直接获取用户名。
//
// GET: /Portfolio/Create

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

//
// POST: /Portfolio/Create

[HttpPost]
public ActionResult Create(Portfolio portfolio)
{
    if (ModelState.IsValid)
    {
        // TODO:
        // Here I need the UserId to associate the new portfolio with the user!
        // I don't want to make another LINQ query to get the UserId from Username.
        // ...

        _db.Portfolios.Add(portfolio);
        _db.SaveChanges();
        return RedirectToAction("Index");
    }

    return View(portfolio);
}