0

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

Database tables

My View on the browser

Browser view of the dropdown list

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?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459

1 Answers1

1

When login is successful then pass the userid while redirecting to next page. Use that userid in the next page get the dropdown values from database(where userid=loggedinuserid). now you will get only emploees corresponding to the current user.

use session to store and use the userid in redirected page. or this link will help you. RedirectToAction with parameter

Kavi
  • 140
  • 1
  • 9
  • If this answer solves your problem or redirecting you to the right direction, then please accept this as answer. this will be useful for future reference. If you have any doubts or difficulty to understand the answer please comment here. thank you – Kavi Sep 24 '20 at 09:06
  • thanks for this, how do i get the id? i have got this so far: 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", "controller", new { @id = }); return RedirectToAction("Create", new RouteValueDictionary( new { controller = "controller", action = "Create", Id = id })) – unknown artist01 Sep 24 '20 at 09:46
  • var databaseUserProfile = db.UserProfiles.Where(x => x.UserName == log.UserName && x.Password == log.Password); string userId = databaseUserProfile.UserId will give you UserId – Kavi Sep 24 '20 at 09:52
  • thanks again, although the UserID on the string gives this error Severity Code Description Project File Line Suppression State Error CS1061 'int' does not contain a definition for 'UserID' and no accessible extension method 'UserID' accepting a first argument of type 'int' could be found (are you missing a using directive or an assembly reference?) How can i tackle it? – unknown artist01 Sep 24 '20 at 10:12
  • var userId = db.UserProfiles.FirstOrDefault(x => x.UserName == log.UserName && x.Password == log.Password).UserID; try this inside the success if condition(count>0) – Kavi Sep 24 '20 at 10:19
  • yes now is working but i am getting an error on the razor view. This is my controller public ActionResult Create(UserProfile log, string Id) { ViewBag.UserID = new SelectList(Id, "UserID", "Employer"); return View(); } – unknown artist01 Sep 24 '20 at 12:11