【问题标题】:populating dropdown menu from database in ASP.NET MVC [duplicate]从 ASP.NET MVC 中的数据库填充下拉菜单 [重复]
【发布时间】:2019-08-20 18:16:36
【问题描述】:

我希望在 3 个不同的下拉菜单(患者、医生、诊所)中选择一些选项后创建一个“约会”

我在创建和填充这 3 个下拉菜单方面需要帮助。

我对 ASP.NET MVC 和 C# 还是很陌生。因此,非常感谢您的帮助。

我将包含约会控制器和约会创建视图代码。

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();
            }
        }
    }
}

约会“创建”视图

 @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")
}

【问题讨论】:

  • 我不确定我是否理解,但该代码通常会在布局内。我建议您首先仅使用 html 构建菜单。这样就更容易找到用代码构建它的方法了。
  • 问题到底出在哪里?我不清楚您要在哪里填充下拉列表。
  • 我在当前视图中添加了一个屏幕截图,而不是常规输入,我想要从数据库中填充真实医生、患者和诊所的下拉菜单。

标签: c# asp.net-mvc


【解决方案1】:

如果下拉菜单选项在数据库中,为什么不在模型中添加一个列表,在 GET ActionMethod 中填充该列表,然后使用 DropdownListFor 辅助标记呈现它。 比如……

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

在控制器中,您可以查询数据库中的选项

    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);
    }

像以前一样,您必须对所有字段执行此操作。如果您担心需要多次往返数据库来获取值,请对 Z.EntityFrameWork nuget 软件包进行一些研究,该软件包可让您运行批处理 SQL 语句,这样您就可以在一轮数据库中获得所有三个结果旅行。

那么在视图中,你可以这样做……

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

【讨论】:

    【解决方案2】:

    在创建控制器 GET 中,您应该创建 3 个类似的视图包

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

    在视图中,使用下拉列表:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-07-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-05-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多