首页 > 解决方案 > 在 MVC 中设置数据绑定 Dropdownlist 的默认值

问题描述

我有一个数据表,如下图所示。我希望当用户单击编辑按钮时,下拉列表中的默认值是来自数据库的值

在此处输入图像描述

职业治疗应该设置为下拉列表的默认值,但目前我得到了一些不同的东西,如下图所示

在此处输入图像描述

我尝试使用占位符或在视图中选择但没有运气

看法

   <div class="form-group">
                    @Html.LabelFor(model => model.Directorate, htmlAttributes: new { @class = "control-label col-md-2" })
                    <div class="col-md-10">
                        @*@Html.EditorFor(model => model.Directorate, new { htmlAttributes = new { @class = "form-control" } })*@
                        @Html.DropDownListFor(model => model.Directorate,
                                new List<SelectListItem> {
                               //new SelectListItem { Value = "" , Text = "Select Directorate" },
                              new SelectListItem { Value = "EC" , Text = "EC - ELDERLY CARE" },
                              new SelectListItem { Value = "LD" , Text = "LD - LEARNING DISABILITIES" },
                              new SelectListItem { Value = "MH" , Text = "MH - MENTAL HEALTH" },
                              new SelectListItem { Value = "PC" , Text = "PC - PRIMARY CARE" },
                           }, new { @class = "form-control selectchosen", placeholder = @ViewBag.Directorate })
                        @Html.ValidationMessageFor(model => model.Directorate, "", new { @class = "text-danger" })
                    </div>
                </div>

                <div class="form-group">
                    @Html.LabelFor(model => model.RecordType, htmlAttributes: new { @class = "control-label col-md-2" })
                    <div class="col-md-10">                          
                        @Html.DropDownListFor(model => model.RecordType, new SelectList(ViewBag.RecordType, "Code", "Name",ViewBag.RecordType1 ),
                       new { @class = "form-control selectchosen"/*, @Selected = @ViewBag.RecordType1*/ } )
                        @Html.ValidationMessageFor(model => model.RecordType, "", new { @class = "text-danger" })
                    </div>
                </div>

控制器获取

 public ActionResult Edit(int id)
    {
        var patientid = Session["patientid"];
        ViewBag.Record = id;
        ViewBag.patientid = patientid;
        HealthRecordView recordView = new HealthRecordView();
        DataTable dtblRecord = new DataTable();

       // Bind the Database value to the dropdown
        RecordType();

        using (OracleConnection conn = new OracleConnection(WebConfigurationManager.ConnectionStrings["HealthRecord"].ConnectionString))
        {
            OracleCommand cmd = conn.CreateCommand();
            cmd.CommandText = @"SELECT
                                            hr.record,
                                            hr.patient,
                                            hr.directorate,
                                            hrt.description AS recordtype,
                                            hr.period_start,
                                            hr.period_end,
                                            al.Name as Location,
                                            aly.Name as storage_point,
                                            hr.withdrawn,
                                            mt.description as Mediatype,
                                            hrt.CODE as RecordtypeId,
                                            mt.CODE as MediatypeId,
                                            hr.STORAGE_POINT as StorageId,
                                            hr.LOCATION as LocationId

                                        FROM
                                            health_records hr
                                            LEFT JOIN health_record_types hrt ON hr.record_type = hrt.code  AND hrt.directorate = hr.directorate
                                            LEFT JOIN media_types mt ON hr.media_type = mt.code
                                            LEFT JOIN cht.agent_locations al on hr.location = al.location and al.name is not null and al.invalidated is null
                                            LEFT JOIN cht.agent_locations aly on hr.storage_point = aly.location  and aly.name is not null  and aly.invalidated is null
                                         Where hr.record =:Record";
            cmd.CommandType = CommandType.Text;
            cmd.Parameters.Add("Record", id);
            OracleDataAdapter adapter = new OracleDataAdapter(cmd);
            adapter.Fill(dtblRecord);
        }
        if (dtblRecord.Rows.Count == 1)
        {
            recordView.Record = dtblRecord.Rows[0][0].ToString();
            recordView.Patient = dtblRecord.Rows[0][1].ToString();
            recordView.Directorate = dtblRecord.Rows[0][2].ToString();
            ViewBag.Directorate= dtblRecord.Rows[0][2].ToString();
            recordView.RecordType = dtblRecord.Rows[0][3].ToString();
            ViewBag.RecordType1 = dtblRecord.Rows[0][3].ToString();
            return View(recordView);
        }          
        return View();
    }

绑定下拉值

   public List<DropdownClass> RecordType()
    {
        List<DropdownClass> RecordType = new List<DropdownClass>();

        using (OracleConnection conn = new OracleConnection(WebConfigurationManager.ConnectionStrings["HealthRecord"].ConnectionString))
        {
            conn.Open();
            OracleCommand cmd = new OracleCommand();

            cmd.Connection = conn;
            cmd.CommandText = @"select code, code ||' '||'-'||' '|| description as name  from cht.health_record_types where subtype||'X' != 'YX' order by code ";
            cmd.CommandType = CommandType.Text;
            OracleDataReader rdr = cmd.ExecuteReader();
            while (rdr.Read())
            {
                var RecordTypedata = new DropdownClass();
                RecordTypedata.Name = rdr["name"].ToString();
                RecordTypedata.Code = rdr["code"].ToString();
                RecordType.Add(RecordTypedata);
            }
        }
        return (RecordType);
    }

下拉模型

 public class DropdownClass
{
    public string Name { get; set; }
    public string Code { get; set; }
}

标签: c#asp.net-mvc

解决方案


推荐阅读