0

I have a dropdown in which datas are binding from database. its works fine in which I need to display one particular item a first value in the dropdown. For example dropdown with arun,siva,kumar in which siva must be at the first place note three datas coming from database. View:

@using (Html.BeginForm()) {

    <fieldset>

        <legend>Multi-Select Demo</legend>

        <div class="editor-field">

           @Html.DropDownList("FirstName", Model.SelectNameList);

            )

        </div>

        <p>

            <input type="submit" value="Save" />

        </p>

    </fieldset>

}

Controller

  public ActionResult List()
        {
            SqlConnection conn = new SqlConnection(con);
            Patient model = new Patient();
            List<Patient> objUserDetails = new List<Patient>();
            DataTable dt = new DataTable();
            conn.Open();
            SqlCommand cmd = new SqlCommand("select * from Patients", conn);
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            da.Fill(dt);
            var studentCount = dt.Rows.Count;
            if (dt.Rows.Count > 0)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    Patient userinfo = new Patient();
                    userinfo.FirstName = dt.Rows[i]["FirstName"].ToString();
                    userinfo.PatientId = Convert.ToInt16(dt.Rows[i]["PatientId"]);
                    objUserDetails.Add(userinfo);
                }
            }

            model.SelectNameList = new SelectList(objUserDetails, "PatientId", "FirstName");

            return View(model);
        }

I want the sixth data of my list as first data in dropdown

MuthuKumar
  • 7
  • 1
  • 5

1 Answers1

0

One option can be to use jquery. You can set id to your dropdownlist and then use that id in document.ready handler to set selected index for dropdownlist

Assign Id to your dropdownlist

@Html.DropDownList("FirstName", Model.SelectNameList,new { id = "drpdwnID" });

Jquery Code

$(document).ready(function(){
$("#drpdwnID option:eq(4)").attr('selected', true)//4 is the index you want to set as selected
})
Nitin Varpe
  • 10,450
  • 6
  • 36
  • 60