问题
To learn the ASP.NET MVC 4 patter I'm developing and application that has to manage some data and user content.
This is the database:
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; }
Where relationship cardinalities are:
- UserProfile (a user) - Portfolio: 1-N
- UserProfile - Deposit: 1-N
- Portfolio - Fund: N-M
- Fund - Source: 1-N
- Fund - Quote: 1-N
- Fund - Deposit: 1-N
On the database there are also the ASP.NET Simple Membership tables: webpages_Membership, webpages_OAuthMembership, webpages_Roles, webpages_UsersInRoles.
UserProfile Model:
[Table("UserProfile")]
public class UserProfile
{
[Key]
[DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
public int UserId { get; set; }
public string UserName { get; set; }
}
Portfolio Model:
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
}
Said that, this is what the application should make the user do:
- register/login;
- see/modify/create only is own portfolios/deposits;
- manage funds and portfolios;
- and for now this is almost all...
I post here my actual implementation of the Index action of Portfolio Controller. I need the Username to get the UserId that is the foreign key on the Portfolio table, to get only the specific user's portfolios. Then I use LINQ to make the query.
[Authorize]
public class PortfolioController : Controller
{
private readonly MyDb _db = new MyDb();
public ActionResult Index()
{
string userName = null;
if (HttpContext.User.Identity.IsAuthenticated)
{
userName = HttpContext.User.Identity.Name;
}
var result = _db.UserProfiles
.Where(u => u.UserName.Equals(userName))
.Join(_db.Portfolios, u => u.UserId, p => p.UserId, (u, p) => p);
return View(result);
}
}
So, what about this approach? I already know that probably it's not the best solution...
Then, in the Create action I need the UserId to associate the new portfolio with the correct user, so what have I to do?
//
// 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);
}
Of course, I need to make the same thing with the Deposit model, as the user create a new deposit and has to see/edit only his own deposits.
What I want to know is:
Which is the best pattern (most correct/elegant) to manage the user content and the needs of Username/UserId on many controller actions and views?
来源:https://stackoverflow.com/questions/14630158/asp-net-mvc-4-how-to-manage-userid-and-user-content-inside-controllers