C# 在MVC中设置数据绑定下拉列表的默认值

C# 在MVC中设置数据绑定下拉列表的默认值,c#,asp.net-mvc,C#,Asp.net Mvc,我有一个数据表,如下图所示。我希望当用户单击编辑按钮时,dropdownlist中的默认值是来自数据库的值 作业治疗应设置为dropdownlist的默认值,但目前我得到的结果与下图所示不同 我尝试在视图中使用占位符或选定项,但运气不佳 看法 绑定下拉值 public List<DropdownClass> RecordType() { List<DropdownClass> RecordType = new List<Dropdo

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

作业治疗应设置为dropdownlist的默认值,但目前我得到的结果与下图所示不同

我尝试在视图中使用占位符或选定项,但运气不佳

看法

绑定下拉值

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

这回答了你的问题吗@Dani谢谢你的回复我在提交问题之前检查了这个
 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; }
}