title: ASP .NET MVC 4 Put join query result inside viewmodel and pass it to a view
body: <p>I'm new to MVC and I need to bright up my mind about some concept of ASP .NET MVC.</p>
  
  <p>In my spare time I'm trying to learn more about MVC building a web application to manage some user content. For more details, you can see my previous question about the same application <a href="https://stackoverflow.com/questions/12458683/asp-net-mvc-how-to-manage-user-content-using-asp-net-membership-provider">here</a>.</p>
  
  <p>Now I'm stuck on a controller action that should pass a view model to a view and I need to know a couple o things. <strong>So I'd like to know if and where I'm doing wrong</strong>.</p>
  
  <p>1) In the <strong>PortafoglioIndexViewModel</strong> I added a User property to get the Username inside the view. In the View I call <code>Model.Username</code> <strong>to show the username on the page</strong>. It works but since I remove the <code>IEnumerable&lt;&gt;</code> from the <code>@model</code> directive I cannot iterate through the object anymore. <strong>Intellisense markup in red my code</strong> <code>@Html.DisplayNameFor(model =&gt; model...)</code> and <code>@Html.DisplayFor(modelItem =&gt; item...)</code> inside the <code>foreach</code> loop.</p>
  
  <p>If I change <code>@model</code> with <code>IEnumerable&lt;FundMonitor.Web.ViewModels.PortafoglioIndexViewModel&gt;</code>, then I can iterate through object but if I call <code>Model.Username</code> I no longer get the object properties since it is an IEnumerable. So in this case the only way I found to <strong>get the Username property</strong> is to add <code>@using FundMonitor.Web.ViewModels</code> at the top of the view and then make a cast like <code>((PortafoglioIndexViewModel)Model).Username</code> but I don't think that would be a best practice...</p>
  
  <p>So how can I get goat and cabbage?</p>
  
  <p>2) Is my way of <strong>populating and generating the View Model</strong> inside the <code>Index</code> action right?</p>
  
  <p>3) What would be a better way to get the username/userid since I need them inside almost every controllers to check the user identity and <strong>get only the user's right record on the db</strong>?</p>
  
  <p><strong>The Controller Action:</strong></p>
  
  <pre><code>[Authorize]
  public class PortafoglioController : Controller
  {
      private readonly FundMonitorDb _db = new FundMonitorDb();
  
      //
      // GET: /Portafoglio/
  
      public ActionResult Index()
      {
          string userName = null;
  
          if (HttpContext.User.Identity.IsAuthenticated)
          {
              userName = HttpContext.User.Identity.Name;
          }
  
          var result = from p in _db.Portafogli
                       join u in _db.UserProfiles on p.UserId equals u.UserId
                       where u.UserName.Equals(userName)
                       select new PortafoglioIndexViewModel
                           {
                               Id = p.Id,
                               DataCreazione = p.DataCreazione,
                               Etichetta = p.Etichetta,
                               NumeroFondi = p.Fondi.Count(),
                               Username = userName
                           };
  
          return View(result);
      }
  }
  </code></pre>
  
  <p><strong>The View Model:</strong></p>
  
  <pre><code>public class PortafoglioIndexViewModel
  {
      public int Id { get; set; }
  
      [Display(Name = "Etichetta")]
      public string Etichetta { get; set; }
  
      [Display(Name = "Creato il"), DisplayFormat(DataFormatString = "{0:d}", ApplyFormatInEditMode = true)]
      public DateTime DataCreazione { get; set; }
  
      [Display(Name = "N. fondi")]
      public int NumeroFondi { get; set; }
  
      public string Username { get; set; }
  }
  </code></pre>
  
  <p><strong>The View:</strong></p>
  
  <pre><code>@model FundMonitor.Web.ViewModels.PortafoglioIndexViewModel
  
  @{
      ViewBag.Title = "Portafogli di " + Model.Username;
  }
  
  &lt;h2&gt;I miei portafogli&lt;/h2&gt;
  
  &lt;p&gt;
      @Html.ActionLink("Crea nuovo", "Create")
  &lt;/p&gt;
  &lt;table&gt;
      &lt;tr&gt;
          &lt;th&gt;
              @Html.DisplayNameFor(model =&gt; model.Etichetta)
          &lt;/th&gt;
          &lt;th&gt;
              @Html.DisplayNameFor(model =&gt; model.DataCreazione)
          &lt;/th&gt;
          &lt;th&gt;
              @Html.DisplayNameFor(model =&gt; model.NumeroFondi)
          &lt;/th&gt;
          &lt;th&gt;&lt;/th&gt;
      &lt;/tr&gt;
  
  @foreach (var item in Model) {
      &lt;tr&gt;
          &lt;td&gt;
              @Html.DisplayFor(modelItem =&gt; item.Etichetta)
          &lt;/td&gt;
          &lt;td&gt;
              @Html.DisplayFor(modelItem =&gt; item.DataCreazione)
          &lt;/td&gt;
          &lt;td&gt;
              @Html.DisplayFor(modelItem =&gt; item.NumeroFondi)
          &lt;/td&gt;
          &lt;td&gt;
              @Html.ActionLink("Edit", "Edit", new { id=item.Id }) |
              @Html.ActionLink("Details", "Details", new { id=item.Id }) |
              @Html.ActionLink("Delete", "Delete", new { id=item.Id })
          &lt;/td&gt;
      &lt;/tr&gt;
  }
  
  &lt;/table&gt;
  </code></pre>