【发布时间】:2021-01-10 12:27:22
【问题描述】:
我是 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();
}
}
最后,在下拉列表中,我希望看到关联的雇主,因此根据表关系将其级联给用户。如果其他雇主与已登录的用户无关,我不想看到他们。实现这一目标的最佳方法是什么?
【问题讨论】:
标签: c# asp.net-mvc asp.net-core asp.net-mvc-4