Authentication NET MVC 4将联接查询结果放入viewmodel中,并将其传递给视图

Authentication NET MVC 4将联接查询结果放入viewmodel中,并将其传递给视图,authentication,asp.net-mvc-4,viewmodel,identity,Authentication,Asp.net Mvc 4,Viewmodel,Identity,我是MVC新手,我需要对ASP.NETMVC的一些概念有一点想法 在我的业余时间,我试图学习更多关于MVC构建一个web应用程序来管理一些用户内容的知识。有关更多详细信息,请参见我之前关于同一应用程序的问题 现在我被一个控制器动作卡住了,它应该把一个视图模型传递给一个视图,我需要知道一些事情所以我想知道我是否做错了,哪里做错了 1) 在portafgloindexviewmodel中,我添加了一个User属性,以获取视图中的用户名。在视图中,我调用Model.Username在页面上显示用户名。

我是MVC新手,我需要对ASP.NETMVC的一些概念有一点想法

在我的业余时间,我试图学习更多关于MVC构建一个web应用程序来管理一些用户内容的知识。有关更多详细信息,请参见我之前关于同一应用程序的问题

现在我被一个控制器动作卡住了,它应该把一个视图模型传递给一个视图,我需要知道一些事情所以我想知道我是否做错了,哪里做错了

1) 在portafgloindexviewmodel中,我添加了一个User属性,以获取视图中的用户名。在视图中,我调用
Model.Username
在页面上显示用户名。它可以工作,但由于我从
@model
指令中删除了
IEnumerable
,因此我无法再遍历该对象Intellisense红色标记“我的代码”
@Html.DisplayNameFor(model=>model…)
@Html.DisplayFor(modelItem=>item…)
foreach
循环中

如果我用
IEnumerable
更改
@model
,那么我可以遍历对象,但如果调用
model.Username
我将不再获取对象属性,因为它是IEnumerable。因此,在本例中,我发现获取用户名属性的唯一方法是使用FundMonitor.Web.ViewModels在视图顶部添加
,然后像
((PortaFoglionDefexViewModel)模型)那样进行转换。用户名
,但我认为这不是最佳做法

那我怎么才能得到山羊和卷心菜呢

2) 我在
索引中填充和生成视图模型的方法正确吗

3) 由于我几乎在每个控制器中都需要用户名/用户ID来检查用户身份和在数据库中仅获取用户正确的记录,因此,有什么更好的方法来获取用户名/用户ID

控制器操作:

[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);
    }
}
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; }
}
@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>
视图模型:

[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);
    }
}
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; }
}
@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>
视图:

[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);
    }
}
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; }
}
@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>
@model FundMonitor.Web.ViewModels.portafgloindexviewmodel
@{
ViewBag.Title=“portafoli di”+Model.Username;
}
我是miei Portafoli

@ActionLink(“Crea nuovo”,“Create”)

@DisplayNameFor(model=>model.Etichetta) @DisplayNameFor(model=>model.DataCreazione) @DisplayNameFor(model=>model.NumeroFondi) @foreach(模型中的var项目){ @DisplayFor(modelItem=>item.Etichetta) @DisplayFor(modelItem=>item.DataCreazione) @DisplayFor(modelItem=>item.NumeroFondi) @ActionLink(“编辑”,“编辑”,新的{id=item.id})| @ActionLink(“详细信息”,“详细信息”,新的{id=item.id})| @ActionLink(“删除”,“删除”,新的{id=item.id}) }
一种方法是创建一个包含用户详细信息的ViewModel和一个
PortafoglioIndexViewModel的
IEnumerable
,如下所示:

步骤1:创建新包装,
WrapperViewModel
(作为示例名称):

步骤3:需要更改视图以适应新的
包装视图模型

@model FundMonitor.Web.ViewModels.WrapperViewModel

@{
    ViewBag.Title = "Portafogli di " + Model.Username;
}

<h2>I miei portafogli</h2>

<p>
    @Html.ActionLink("Crea nuovo", "Create")
</p>

@if (Model.Children != null)
{
    <table>
        <tr>
            <th>
                @Html.DisplayNameFor(model => model.Children.First().Etichetta)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.Children.First().DataCreazione)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.Children.First().NumeroFondi)
            </th>
            <th></th>
        </tr>

        @foreach (var item in Model.Children)
        {
            <tr>
                <td>
                    @Html.DisplayFor(x => item.Etichetta)
                </td>
                <td>
                    @Html.DisplayFor(x => item.DataCreazione)
                </td>
                <td>
                    @Html.DisplayFor(x => 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>
}
@model FundMonitor.Web.ViewModels.WrapperViewModel
@{
ViewBag.Title=“portafoli di”+Model.Username;
}
我是miei Portafoli

@ActionLink(“Crea nuovo”,“Create”)

@if(Model.Children!=null) { @DisplayNameFor(model=>model.Children.First().Etichetta) @DisplayNameFor(model=>model.Children.First().DataCreazione) @DisplayNameFor(model=>model.Children.First().NumeroFondi) @foreach(Model.Children中的var项) { @DisplayFor(x=>item.Etichetta) @DisplayFor(x=>item.DataCreazione) @DisplayFor(x=>item.NumeroFondi) @ActionLink(“编辑”,“编辑”,新的{id=item.id})| @ActionLink(“详细信息”,“详细信息”,新的{id=item.id})| @ActionLink(“删除”,“删除”,新的{id=item.id}) } }

希望这能为您提供一些指导…

它应该可以工作,但这似乎是一个有点笨拙的解决方案。因为我会有很多视图模型,所以为它们中的每一个创建一个新的视图模型包装器将很快增加类的数量,同时也会增加代码维护的难度。我更喜欢“最佳实践”解决方案,因为我想学习MVC模式。这本质上就是MVC模式——每个视图的视图模型。如果您正确地构造模型和查看模型,代码维护应该不会太困难!这里有另一条线索可以解决您的问题:(注意一个比我更具代表性的家伙的第一条评论:)。是的,我确实想为每个视图创建一个视图模型。但是您在我的
portafgloindexviewmodel
上创建了一个
WrapperViewModel
,它已经是
portafglio
模型的
索引
视图的视图模型。有一件事:我遗漏了一些可能会引起混淆的东西-删除
公共字符串Username{get;set;}
从您的viewmodel和LINQ查询中的
选择新PortafoglioIndexViewModel
中的
Username=Username
,因为它们在使用新包装时是多余的。这能澄清一点吗?不完全是。。。但是我