如何获取登录用户的下拉列表

时间:2016-08-25 20:38:16

标签: asp.net asp.net-mvc razor asp.net-identity

如何获取当前登录用户的下拉列表

它显示了我数据库中完整表记录的列表

我希望它显示我所属的用户名单

我的控制器位于

之下
 [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);
    }

我的观点是这个

<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>

请仔细阅读我的问题.....我要求只在 登录用户 的DROPDOWN LIST中显示记录列表。 .............这些答案显示了所有用户

所做的完整列表

2 个答案:

答案 0 :(得分:0)

在你控制中你应该:

var DoctorsList = db.Doctors.Select(Doctor=> new { Text = Doctor.FullName, Value =     
    Doctor.Doctor_ID }).OrderBy(Doctor=> Doctor.Doctor_ID );
                    ViewBag.DoctorsList = new SelectList(DoctorsList , "Value", "Text");

在视图中你应该:

 @Html.DropDownListmodel => model.Doctor_ID , new SelectList(ViewBag.DoctorsList , "Value", "Text"), "--- Select Doctor---", new {@class = "form-control" }) 

答案 1 :(得分:0)

控制器中的

ViewBag.DoctorsList = new SelectList(db.Doctors, "Doctor_ID", "FullName");

视图中的

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