I'm new to MVC and I need to bright up my mind about some concept of ASP .NET MVC.
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 here.
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. So I'd like to know if and where I'm doing wrong.
1) In the PortafoglioIndexViewModel I added a User property to get the Username inside the view. In the View I call Model.Username
to show the username on the page. It works but since I remove the IEnumerable<>
from the @model
directive I cannot iterate through the object anymore. Intellisense markup in red my code @Html.DisplayNameFor(model => model...)
and @Html.DisplayFor(modelItem => item...)
inside the foreach
loop.
If I change @model
with IEnumerable<FundMonitor.Web.ViewModels.PortafoglioIndexViewModel>
, then I can iterate through object but if I call Model.Username
I no longer get the object properties since it is an IEnumerable. So in this case the only way I found to get the Username property is to add @using FundMonitor.Web.ViewModels
at the top of the view and then make a cast like ((PortafoglioIndexViewModel)Model).Username
but I don't think that would be a best practice...
So how can I get goat and cabbage?
2) Is my way of populating and generating the View Model inside the Index
action right?
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 get only the user's right record on the db?
The Controller Action:
[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);
}
}
The View Model:
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; }
}
The View:
@model FundMonitor.Web.ViewModels.PortafoglioIndexViewModel
@{
ViewBag.Title = "Portafogli di " + Model.Username;
}
<h2>I miei portafogli</h2>
<p>
@Html.ActionLink("Crea nuovo", "Create")
</p>
<table>
<tr>
<th>
@Html.DisplayNameFor(model => model.Etichetta)
</th>
<th>
@Html.DisplayNameFor(model => model.DataCreazione)
</th>
<th>
@Html.DisplayNameFor(model => model.NumeroFondi)
</th>
<th></th>
</tr>
@foreach (var item in Model) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.Etichetta)
</td>
<td>
@Html.DisplayFor(modelItem => item.DataCreazione)
</td>
<td>
@Html.DisplayFor(modelItem => item.NumeroFondi)
</td>
<td>
@Html.ActionLink("Edit", "Edit", new { id=item.Id }) |
@Html.ActionLink("Details", "Details", new { id=item.Id }) |
@Html.ActionLink("Delete", "Delete", new { id=item.Id })
</td>
</tr>
}
</table>