Search code examples
asp.netasp.net-mvcrazorasp.net-identity

how to get dropdown list for the logged in user


how to get list in dropdown for the current logged in user

it shows me the list of complete table records from my database

i want it to show my the list of only that belongs to the user logged in

my Controller is below

 [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Create([Bind(Include = "ID,ClinicName,Surgery_ID,Doctor_ID,Patient_ID,Procedure_Code,Appointment_Date,From_Time,To_Time,Status,SMS,Comments")] Schedule schedule)
    {
        if (ModelState.IsValid)
        {
            schedule.ClinicName = User.Identity.GetUserId();
            db.Schedules.Add(schedule);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        var currentUser = User.Identity.GetUserId();
        var schedules = db.Schedules.Where(x => x.ClinicName == currentUser);
        ViewBag.CurrentUser = currentUser;
        // ViewBag.ClinicName = new SelectList(db.AspNetUsers, "Id", "FirstName", schedule.ClinicName);
        ViewBag.Doctor_ID = new SelectList(db.Doctors, "Doctor_ID", "FullName", schedule.Doctor_ID);
        ViewBag.Patient_ID = new SelectList(db.Patients, "Patient_ID", "First_Name", schedule.Patient_ID);
        ViewBag.Surgery_ID = new SelectList(db.Surgeries, "Surgery_ID", "Surgery_Name", schedule.Surgery_ID);
        return View(schedule);
    }

and my view is this

<div class="form-group">
            @Html.LabelFor(model => model.Surgery_ID, "Surgery_ID", htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.DropDownList("Surgery_ID", null, htmlAttributes: new { @class = "form-control" })
                @Html.ValidationMessageFor(model => model.Surgery_ID, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.Doctor_ID, "Doctor_ID", htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.DropDownList("Doctor_ID", null, htmlAttributes: new { @class = "form-control" })
                @Html.ValidationMessageFor(model => model.Doctor_ID, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.Patient_ID, "Patient_ID", htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.DropDownList("Patient_ID", null, htmlAttributes: new { @class = "form-control" })
                @Html.ValidationMessageFor(model => model.Patient_ID, "", new { @class = "text-danger" })
            </div>
        </div>

PLEASE READ MY Question.....i am asking for a LIST of Records to be SHOWN in DROPDOWN LIST made only by the LOGGED-IN USER...............these answers are showing the complete LIST made by ALL USERS


Solution

  • in the controller

    var doctor= db.doctor.ToList().Where(d=>d.UserId==currentUser ); 
    ViewBag.DoctorsList = new SelectList(doctor, "Doctor_ID","FullName");

    in the view

    @Html.DropDownList("Doctor_ID",ViewBag.DoctorsList as SelectList,, htmlAttributes: new { @class = "form-control" })