C# 如何在MVC5 web视图中使用模型变量?

C# 如何在MVC5 web视图中使用模型变量?,c#,asp.net-mvc-5,C#,Asp.net Mvc 5,我已经从active directory中提取了一个用户列表,这些用户向当前用户提供了活动报告,并且已经成功地在MVC 5的web视图表中显示了该列表 但是,当我尝试在web视图的表上方打印当前用户的名称时,遇到了一个问题 有人知道如何使用razor视图在MVC5中实现这一点吗 这是我用来从系统中获取当前用户的代码: public string getUserName() { displayName = System.Security.Principal.WindowsIdentity.

我已经从active directory中提取了一个用户列表,这些用户向当前用户提供了活动报告,并且已经成功地在MVC 5的web视图表中显示了该列表

但是,当我尝试在web视图的表上方打印当前用户的名称时,遇到了一个问题

有人知道如何使用razor视图在MVC5中实现这一点吗

这是我用来从系统中获取当前用户的代码:

public string getUserName()
{
    displayName = System.Security.Principal.WindowsIdentity.GetCurrent().Name;
    return displayName;
}
我目前正在研究的观点是:

@model IEnumerable<BUUK.BSS.Models.User>

@{
    ViewBag.Title = "MyTeam";
}
<h2>@ViewBag.Title</h2>
<h3>@ViewBag.Message</h3>

<p>
    ViewBag.Title = System.Security.Principal.WindowsIdentity.GetCurrent().Name;
</p>
<table class="table">
    <tr>
        <th>
            @Html.DisplayNameFor(model => model.Name)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Email)
        </th>
        <th></th>
    </tr>

    @foreach (var item in Model)
    {
        <tr>
            <td>
                @Html.DisplayFor(modelItem => item.Name)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Email)
            </td>
            @*<td>
                @Html.ActionLink("Edit", "Edit", new { /* id=item.PrimaryKey */ }) |
                @Html.ActionLink("Details", "Details", new { /* id=item.PrimaryKey     */ }) |
                @Html.ActionLink("Delete", "Delete", new { /* id=item.PrimaryKey */   })
            </td>*@
        </tr>
    }

</table>
@model IEnumerable
@{
ViewBag.Title=“MyTeam”;
}
@视图包。标题
@查看包。留言

ViewBag.Title=System.Security.Principal.WindowsIdentity.GetCurrent().Name;

@DisplayNameFor(model=>model.Name) @DisplayNameFor(model=>model.Email) @foreach(模型中的var项目) { @DisplayFor(modelItem=>item.Name) @DisplayFor(modelItem=>item.Email) @* @ActionLink(“编辑”,“编辑”,新的{/*id=item.PrimaryKey*/})| @ActionLink(“详细信息”,“详细信息”,新的{/*id=item.PrimaryKey*/})| @ActionLink(“删除”,“删除”,新的{/*id=item.PrimaryKey*/}) *@ }
如果您只是想在视图中打印名称,您应该这样做:

<p>
    @System.Security.Principal.WindowsIdentity.GetCurrent().Name
</p>

@System.Security.Principal.WindowsIdentity.GetCurrent().Name


您能从视图中发布一些代码吗?另外,请更具体地说明您遇到的问题。您是否收到任何编译错误或运行时异常消息?如果我错了,请纠正我,但您实际在哪里使用
getUserName()
?它看起来在你发布的代码中的任何地方都没有使用。它是在一个方法中调用的,该方法从有效的目录中获取列表。谢谢,你可以这样做吗?调用C#方法?是的,你可以。只需将@放在方法调用之前。如何将对象引用添加到方法调用?到目前为止,我已经添加了以下代码:“@BUUK.BSS.Models.User.getUserName();”