Note that there are some explanatory texts on larger screens.

plurals
  1. POASP.NET MVC 4 How to manage userid and user content inside controllers
    text
    copied!<p>To learn the ASP.NET MVC 4 patter I'm developing and application that has to manage some data and user content.</p> <p>This is the database:</p> <pre><code>public DbSet&lt;UserProfile&gt; UserProfiles { get; set; } public DbSet&lt;Fund&gt; Funds { get; set; } public DbSet&lt;Source&gt; Sources { get; set; } public DbSet&lt;Portfolio&gt; Portfolios { get; set; } public DbSet&lt;Quote&gt; Quotes { get; set; } public DbSet&lt;Deposit&gt; Deposits { get; set; } </code></pre> <p>Where relationship cardinalities are:</p> <ul> <li>UserProfile (a user) - Portfolio: 1-N</li> <li>UserProfile - Deposit: 1-N</li> <li>Portfolio - Fund: N-M</li> <li>Fund - Source: 1-N</li> <li>Fund - Quote: 1-N</li> <li>Fund - Deposit: 1-N</li> </ul> <p>On the database there are also the ASP.NET Simple Membership tables: webpages_Membership, webpages_OAuthMembership, webpages_Roles, webpages_UsersInRoles.</p> <p><strong>UserProfile Model</strong>:</p> <pre><code>[Table("UserProfile")] public class UserProfile { [Key] [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)] public int UserId { get; set; } public string UserName { get; set; } } </code></pre> <p><strong>Portfolio Model</strong>:</p> <pre><code>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&lt;Fund&gt; Funds { get; set; } // Linked Funds } </code></pre> <p>Said that, this is what the application should make the user do:</p> <ul> <li>register/login;</li> <li>see/modify/create only is own portfolios/deposits;</li> <li>manage funds and portfolios;</li> <li>and for now this is almost all...</li> </ul> <p>I post here my actual implementation of the <strong>Index action</strong> of Portfolio Controller. <em>I need the Username to get the UserId that is the foreign key on the Portfolio table</em>, to get only the specific user's portfolios. Then I use LINQ to make the query.</p> <pre><code>[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 =&gt; u.UserName.Equals(userName)) .Join(_db.Portfolios, u =&gt; u.UserId, p =&gt; p.UserId, (u, p) =&gt; p); return View(result); } } </code></pre> <p>So, what about this approach? I already know that probably it's not the best solution...</p> <p>Then, in the <strong>Create action</strong> <em>I need the UserId to associate the new portfolio with the correct user</em>, so what have I to do?</p> <pre><code>// // 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); } </code></pre> <p>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.</p> <p><strong>What I want to know is:</strong></p> <p>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?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload