[英]ASP.NET Populating Dropdown with associated foreign key text value
我是 ASP.NET 的新手,我目前正在努力將我的下拉列表綁定到關聯的外鍵表。
我的控制器
public ActionResult Create()
{
//get current user id
ViewBag.UserID = new SelectList(db.UserProfiles, "UserID", "Employer");
return View();
}
我的看法
<div class="form-group">
@Html.LabelFor(model => model.UserID, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownList("UserID", null, htmlAttributes: new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.UserID, "", new { @class = "text-danger" })
</div>
</div>
我的數據庫表
我在瀏覽器上的視圖
我還創建了一個登錄名,以了解哪些用戶已登錄
[HttpPost]
public ActionResult Index(UserProfile log)
{
var user = db.UserProfiles.Where(x => x.UserName == log.UserName && x.Password == log.Password).Count();
if (user > 0)
{
return RedirectToAction("Create", "Dashboard");
}
else
{
return View();
}
}
總之,在下拉列表中,我希望看到關聯的雇主,因此根據表格關系將其級聯到用戶。 如果其他雇主與登錄的用戶無關,我不想看到其他雇主。實現這一目標的最佳方法是什么?
登錄成功后,在重定向到下一頁時傳遞用戶 ID。 在下一頁中使用該用戶 ID 從數據庫中獲取下拉值(其中 userid=loggedinuserid)。 現在您將只獲得與當前用戶相對應的員工。
使用 session 在重定向頁面中存儲和使用用戶 ID。 或者這個鏈接會幫助你。 帶參數的 RedirectToAction
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.