I am very new to ASP.NET and I am currently struggling with binding my dropdown to the associated foreign key table.
My Controller
public ActionResult Create()
{
//get current user id
ViewBag.UserID = new SelectList(db.UserProfiles, "UserID", "Employer");
return View();
}
My 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>
My DB Table
My View on the browser
I have also created a login in order to know what user has logged in
[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();
}
}
In conclusion on the dropdown list i would like to see the employer associated and therefore cascade it to the user based on the table relationship. I don't want to see the other employers if they are not related to the user that has logged in. What is the best way to achieve that?