如何访问Popover内部的循环变量?

时间:2018-09-19 19:02:02

标签: .net razor

我在ViewBag.Customernames中有2个不同的客户名称。

我正在尝试在引导程序弹出窗口中调用Customernames列表。

GraphService.cs:

public static List<string>  GetCustomerNames(List<ResultsItem> groups)
        {
            List<string> names = new List<string>();

            foreach (var group in groups)
            {
                if (group.DisplayName.StartsWith("Customer:", StringComparison.InvariantCultureIgnoreCase))
                {

                    var split = group.DisplayName.Split(':');
                    if (split.Length > 1)
                        names.Add(split[1]);
                }
            }

            return names;
        }

HomeController.cs

        if (User.Identity.IsAuthenticated)
        {
            try
            {
                // Get users's email.
                //email = email ?? User.FindFirst("preferred_username")?.Value;
                email = email ?? User.FindFirst(ClaimTypes.Email)?.Value;
                ViewData["Email"] = email;
                // Get user's id for token cache.
                var identifier = User.FindFirst(Startup.ObjectIdentifierType)?.Value;
                // Initialize the GraphServiceClient.
                var graphClient = _graphSdkHelper.GetAuthenticatedClient(identifier);
                var groups = await GraphService.GetMyMemberOfGroups(graphClient);
                ViewBag.CustomerNames = GraphService.GetCustomerNames(groups);

              if (ViewBag.CustomerNames.Count > 0)
                {
                        ViewBag.CustomerName = ViewBag.CustomerNames[0];
                } 
                ViewBag.givenName = GraphService.GetGivenName(groups);
                ViewBag.RoleNames = GraphService.GetRoleNames(groups);
                Startup.Roles = ViewBag.RoleNames;
                ViewBag.UserName = User.FindFirst(ClaimTypes.GivenName)?.Value;
                ViewBag.LastName = User.FindFirst(ClaimTypes.Surname)?.Value;
            }

视图

<li>
    @foreach (var i in ViewBag.Customernames)
    {
       <a href="#" id="userAvatar" data-container="body" data-toggle="popover" data-trigger="focus" title="@(ViewBag.UserName) @(ViewBag.LastName)" data-content="Customer : @i <hr/><br/><button class='btn'><i class='fa fa-sign-out fa' style='color:red'> </i><a role='button' href='/Account/SignOut' style='text-decoration:none'> LOGOUT</a></button>">
            <strong title="@User.Identity.Name"> @(ViewBag.UserName[0])@(ViewBag.LastName[0])</strong>
       </a>
    }
</li>

运行正常,但是又创建了一个#userAvatar(我知道这不是正确的方法)。

输出

enter image description here

问题:如何仅将foreach用于data-content

1 个答案:

答案 0 :(得分:1)

我得出以下解决方案:

    <li>
        <a href="#" id="userAvatar" data-container="body" data-toggle="popover"
           title="<strong>@(ViewBag.UserName) @(ViewBag.LastName)</strong><br/>@User.Identity.Name"
           data-content="<button class='btn btl-lg btn-block'><i class='fa fa-sign-out fa' style='color:red'> </i><a role='button' href='/Account/SignOut' style='text-decoration:none'> LOGOUT</a></button><hr/><select id='listOfCust' class='btn btn-info btl-lg btn-block'>CUSTOMER : @foreach (var i in ViewBag.CustomerNames){<option id='@i' value='@i' class='btn divider'>@i</option>}</selct><br/>">

            <strong title="@User.Identity.Name"> @(ViewBag.UserName[0])@(ViewBag.LastName[0])</strong>
        </a>
    </li>

在数据内容内部的foreach循环中使用select标签

<select id='listOfCust' class='btn btn-info btl-lg btn-block'>CUSTOMER : @foreach (var i in ViewBag.CustomerNames){<option id='@i' value='@i' class='btn divider'>@i</option>}</selct>