0

I Have a datable as show by the image below. I want when the user click on the edit button the default value from the dropdownlist is the value coming from the database

enter image description here

Occupational Therapy should be set as default value of the dropdownlist but at the moment I am getting something different as shown by the image below

enter image description here

I try to use Placeholder or selected in the view but no luck

View

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

Controller Get

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

Bind Dropdown Value

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

Dropdown Model

 public class DropdownClass
{
    public string Name { get; set; }
    public string Code { get; set; }
}
  • Does this answer your question? [MVC5 - How to set "selectedValue" in DropDownListFor Html helper](https://stackoverflow.com/questions/41719293/mvc5-how-to-set-selectedvalue-in-dropdownlistfor-html-helper) – Dani Dec 17 '19 at 11:53
  • @Dani Thanks for your reply I checked this before I submitted my question – Eric Aime Tchatchoua Dec 17 '19 at 12:01

0 Answers0