2016-05-16 46 views
0

Here is a picture how it lists all phone numbers from all users when I hover over the user account.通userid參數在Foreach循環與酥料餅的內容

我使用ASP.NET創建一個MVC項目,並使用剃刀由「@foreach(以型號VAR項目)」排隊的所有用戶。在該表中,我想要使用一個彈出窗口,該窗口將顯示用戶的電話號碼,該電話號碼一旦懸停在賬戶名稱(稱爲經銷商)上。它目前列出所有用戶記錄下來的所有電話,但我希望它只針對該用戶。我嘗試使用ActionLinks(我如何通過編輯,細節和刪除相同的參數)以及標籤,但似乎都不起作用。我如何傳遞UserID參數來限制彈出窗口只列出鏈接到特定用戶的電話號碼?

下面是表的代碼:

<table> 
@foreach (var item in Model) 
{ 
    <tr> 
     <td> 
      @Html.DisplayFor(modelItem => item.Dealer) 
     </td> 
     <td> 
      <a href="#" data-toggle="popover" title="Phones">@Html.DisplayFor(modelItem => item.UserName)</a> 
      <div id="phone-content" style="display: none"> 
       @foreach (var phone in Model) 
       { 
        //Using ActionLinks 
        @Html.ActionLink("Mobile", "ListPhones", new { id = item.Id })<br /> 
        @Html.ActionLink("Business", "ListPhones", new { id = item.Id })<br /> 
        @Html.ActionLink("Fax", "ListPhones", new { id = item.Id })<br> 

        //Using <a href> tags 
        <a href="tel:@phone.PhoneMobile">@Html.DisplayFor(modelItem => phone.PhoneMobile)</a><br /> 
        <a href="tel:@phone.PhoneBus">@Html.DisplayFor(modelItem => phone.PhoneBus)</a><br /> 
        <a href="tel:@phone.PhoneFax">@Html.DisplayFor(modelItem => phone.PhoneFax)</a><br /> 
       } 
      </div> 
     </td> 
     <td> 
      @Html.ActionLink("Edit", "Edit", new { id = item.Id })<br /> 
      @Html.ActionLink("Details", "Details", new { id = item.Id })<br /> 
      @Html.ActionLink("Delete", "Delete", new { id = item.Id }) 
     </td> 
    </tr> 
} 
</table> 

在控制器中,我創建了一個新的類,列出了手機信息,並試圖傳遞參數那裏。 這裏是在傳遞正確的參數控制器編輯用戶代碼:

public async Task<ActionResult> Edit(string id) 
    { 
     if (id == null) 
     { 
      return new HttpStatusCodeResult(HttpStatusCode.BadRequest); 
     } 
     var user = await UserManager.FindByIdAsync(id); 
     if (user == null) 
     { 
      return HttpNotFound(); 
     } 

     var userRoles = await UserManager.GetRolesAsync(user.Id); 

     return View(new AdminEditUserViewModel() 
     { 
      Id = user.Id, 
      Dealer = user.Dealer, 
      UserName = user.UserName, 
      FirstName = user.FirstName, 
      LastName = user.LastName, 
      Address = user.Address, 
      City = user.City, 
      State = user.State, 
      Zip = user.Zip, 
      Email = user.Email, 
      PhoneMobile = user.PhoneMobile, 
      PhoneBus = user.PhoneBus, 
      PhoneExt = user.PhoneExt, 
      PhoneFax = user.PhoneFax, 
      RolesList = RoleManager.Roles.ToList().Select(x => new SelectListItem() 
      { 
       Selected = userRoles.Contains(x.Name), 
       Text = x.Name, 
       Value = x.Name 
      }) 
     }); 
    } 

    // 
    // POST: /Users/Edit/5 
    [HttpPost] 
    [ValidateAntiForgeryToken] 
    public async Task<ActionResult> Edit([Bind(Include = "Dealer,UserName,FirstName,LastName,Address,City,State,Zip,Email,PhoneMobile,PhoneFax,PhoneBus,PhoneExt,Id")] 
    AdminEditUserViewModel editUser, params string[] selectedRole) 
    { 
     if (ModelState.IsValid) 
     { 
      var user = await UserManager.FindByIdAsync(editUser.Id); 
      if (user == null) 
      { 
       return HttpNotFound(); 
      } 

      user.Dealer = editUser.Dealer; 
      user.UserName = editUser.UserName; 
      user.FirstName = editUser.FirstName; 
      user.LastName = editUser.LastName; 
      user.Address = editUser.Address; 
      user.City = editUser.City; 
      user.State = editUser.State; 
      user.Zip = editUser.Zip; 
      user.Email = editUser.Email; 
      user.PhoneMobile = editUser.PhoneMobile; 
      user.PhoneBus = editUser.PhoneBus; 
      user.PhoneExt = editUser.PhoneExt; 
      user.PhoneFax = editUser.PhoneFax; 

      var userRoles = await UserManager.GetRolesAsync(user.Id); 

      selectedRole = selectedRole ?? new string[] { }; 

      var result = await UserManager.AddToRolesAsync(user.Id, selectedRole.Except(userRoles).ToArray<string>()); 

      if (!result.Succeeded) 
      { 
       ModelState.AddModelError("", result.Errors.First()); 
       return View(); 
      } 
      result = await UserManager.RemoveFromRolesAsync(user.Id, userRoles.Except(selectedRole).ToArray<string>()); 

      if (!result.Succeeded) 
      { 
       ModelState.AddModelError("", result.Errors.First()); 
       return View(); 
      } 
      return RedirectToAction("Index"); 
     } 
     ModelState.AddModelError("", "Something failed."); 
     return View(); 
    } 

對戰的代碼在控制器上市的手機:

public async Task<ActionResult> ListPhones(string id) 
    { 
     if (id == null) 
     { 
      return new HttpStatusCodeResult(HttpStatusCode.BadRequest); 
     } 
     var user = await UserManager.FindByIdAsync(id); 
     if (user == null) 
     { 
      return HttpNotFound(); 
     } 

     return View(new AdminEditUserViewModel() 
     { 
      Id = user.Id, 
      PhoneMobile = user.PhoneMobile, 
      PhoneBus = user.PhoneBus, 
      PhoneExt = user.PhoneExt, 
      PhoneFax = user.PhoneFax 
     }); 
    } 

回答

0

創建一個自定義操作,並嘗試一些如:

<a href="/GetPhoneInfo/@item.Id" data-toggle="popover" title="Phones">@Html.DisplayFor(modelItem => item.UserName)</a> 
+0

感謝您的意見。但問題是我相信href應該是#,這樣彈出窗口可以被點擊而不需要重定向到新頁面。需要設置的方式是,您可以將鼠標懸停在用戶上以查看鏈接到該用戶的所有電話,但是您然後單擊該用戶以使其保持不變,並因此單擊其中一個號碼。我試着玩弄你的建議,但根據我的理解,它總是會重定向到該頁面,而不是打開彈出窗口。 – MAXXtreme

0

像往常一樣,你應該閱讀docs。我假設你正在使用Bootstrap,因爲它看起來看起來就像Bootstrap,默認情況下MVC使用Bootstrap。不過,爲了將來的參考,你應該明確這樣的事情,你也應該以這種方式標記你的問題。

這就是說,文檔告訴你你需要知道的一切。您必須指定將激活彈出窗口的觸發器,幸運的是,Bootstrap的彈出窗口確實允許多個觸發器。對於您的情況,您需要同時使用hoverfocusfocus觸發器會在點擊時打開彈出窗口,然後保持打開狀態直至失去焦點,這看起來正是您想要的。

基本上,你只需要把自己提供的代碼爲「dismissable」酥料餅,並添加額外的hover觸發:

<a tabindex="0" class="btn btn-lg btn-danger" role="button" data-toggle="popover" data-trigger="focus hover" title="Dismissible popover" data-content="And here's some amazing content. It's very engaging. Right?">Dismissible popover</a> 

然後,當然,只是定製內容/班您的需求。

0

demo

我能使其work.send我您的電子郵件示例項目。

+0

太棒了,謝謝!我的電子郵件是[email protected]。 – MAXXtreme