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