0

I have a view to create an 'Appointment' after choosing some options in 3 different drop-down menus (Patient, Doctor, Clinic)

I need help with creating and populating these 3 drop-down menus.

I'm pretty new to ASP.NET MVC and C#. So, your help is most appreciated.

I'll include the appointment controller and appointment creation view code.

AppointmentController

    using System;
    using System.Collections.Generic;
    using System.Data.Entity;
    using System.Linq;
    using System.Web;
    using System.Web.Mvc;
    using ClinicManagement.Models;

    namespace ClinicManagement.Controllers
    {
        public class AppointmentController : Controller
        {
            // GET: Appointment
            public ActionResult Index()
            {
                using (HospitalDatabaseEntities DataBase = new HospitalDatabaseEntities())
                {
                    return View(DataBase.Appointments.ToList());
                }
            }

            // GET: Appointment/Details/5
            public ActionResult Details(int id)
            {
                using (HospitalDatabaseEntities DataBase = new HospitalDatabaseEntities())
                {
                    return View(DataBase.Appointments.Where(x => x.AppintID == id).FirstOrDefault());
                }
            }
        // GET: Appointment/Create
        public ActionResult Create()
        {
            return View();
        }

        // POST: Appointment/Create
        [HttpPost]
        public ActionResult Create(Appointment appointment)
        {
            try
            {
                using (HospitalDatabaseEntities DataBase = new HospitalDatabaseEntities())
                {
                    DataBase.Appointments.Add(appointment);
                    DataBase.SaveChanges();
                }
                // TODO: Add insert logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        // GET: Appointment/Edit/5
        public ActionResult Edit(int id)
        {
            using (HospitalDatabaseEntities DataBase = new HospitalDatabaseEntities())
            {
                return View(DataBase.Appointments.Where(x => x.AppintID == id).FirstOrDefault());
            }
        }

        // POST: Appointment/Edit/5
        [HttpPost]
        public ActionResult Edit(int id, Appointment appointment)
        {
            try
            {
                using (HospitalDatabaseEntities DataBase = new HospitalDatabaseEntities())
                {
                    DataBase.Entry(appointment).State = EntityState.Modified;
                    DataBase.SaveChanges();
                }
                // TODO: Add update logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        // GET: Appointment/Delete/5
        public ActionResult Delete(int id)
        {
            using (HospitalDatabaseEntities DataBase = new HospitalDatabaseEntities())
            {
                return View(DataBase.Appointments.Where(x => x.AppintID == id).FirstOrDefault());
            }
        }

        // POST: Appointment/Delete/5
        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add delete logic here
                using (HospitalDatabaseEntities DataBase = new HospitalDatabaseEntities())
                {
                    Appointment appointment = (DataBase.Appointments.Where(x => x.AppintID == id).FirstOrDefault());
                    DataBase.Appointments.Remove(appointment);
                    DataBase.SaveChanges();
                }
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
    }
}

Appointment 'Create' View

 @model ClinicManagement.Models.Appointment

@{
    ViewBag.Title = "Create";
}

<h2>Create</h2>


@using (Html.BeginForm())
{
    @Html.AntiForgeryToken()

    <div class="form-horizontal">
        <h4>Appointment</h4>
        <hr />
        @Html.ValidationSummary(true, "", new { @class = "text-danger" })


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

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

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

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

        <div class="form-group">
            <div class="col-md-offset-2 col-md-10">
                <input type="submit" value="Create Appointment" class="btn btn-default" />
            </div>
        </div>
    </div>
}

<div>
    @Html.ActionLink("Back to List", "Index")
</div>

@section Scripts {
    @Scripts.Render("~/bundles/jqueryval")
}

This is the current view

3
  • I'm not sure if I understand but that code would normally be inside the layout. I would suggest you first build the menu with html only. Then it'll be easier to figure a way to build it with code.
    – the_lotus
    Aug 20, 2019 at 18:19
  • Where exactly is the problem? I'm not clear on where you're trying to populate a dropdown.
    – Jay Jordan
    Aug 20, 2019 at 19:02
  • I added a screenshot to the current view, instead of regular input, I want drop-down menus that are populated from database with real Doctor, patient and a clinic.
    – Snake
    Aug 20, 2019 at 19:25

2 Answers 2

1

If the drop down menu options are in a database, why not add a list to your model, populate that list in your GET ActionMethod and then render it using the DropdownListFor helper tag. For example...

public class Appointment
{
   public IEnumerable<SelectListItem> Clinic {get; set;}
 //You should add this for every dropdown menu you intend to put in the list.
 //I am guessing you already have a model like this as this was not in the question
}

public class Clinic
{
  public int ClinicId {get; set;}
  public string ClinicName {get; set;}
}

In the controller, you can then query the database for the options

    public ActionResult Create()
    {
        var Clinic = context.Clinic.ToList();
        var model = new Appointments()
        {
           Clinic = Clinic.Select(x => new SelectListItem
           {
             Value = x.ClinicId.ToString(),
             Text = x.ClinicName
           }
        }
        return View(model);
    }

Like before, you would have to do this for all the fields. If you are worried about the numerous roundtrip to the database to get the values, do some research about Z.EntityFrameWork nuget pakages that lets you run batch SQL statements so you can get all three results with one database round trip.

Then in the view, you can do this...

@Html.DropDownListFor(m => m.ClinicId, Model.Clinic, "Select Clinic", new { @class = "form-control", id = "clinic" })
0

In Create controller GET , you should create 3 viewbag like

ViewBag.Patient = Database.Patient.ToList();

...

and in view, use dropdownlist:

@Html.DropDownList("PatientId",  new SelectList(ViewBag.Accounts, "PatientId", "PatientName")), "choose the patient", new { @class = "form-control" }))

Not the answer you're looking for? Browse other questions tagged or ask your own question.