Shyamal
Shyamal

Reputation: 161

Set the selected value for a DropDownList using SelectListItem

Although this question has been asked for several times, still I am struggling to find a solution for a problem

I have a drop down and i want to bind the selected value when retrieving data. here is my controller

studentList = db.Students
                    .Select(x => new SelectListItem
                    {
                        Value = x.StudentId.ToString(),
                        Text = x.StudentNo + " - " + x.StudentNameEn
                    }).ToList();
                ViewData["studentList"] = studentList;

here is my view

 @Html.DropDownList("StudentNo", ViewData["studentList"] as List<SelectListItem>, "---Please Select---", new { @class = "form-control selectpicker", id = "studentIdDrp" })

What I have tried

  1. I tried to bind the value using jquery

    $("#studentIdDrp").val('@Model.AppointmentViewModel.FK_StudentId');
    
  2. I tried from the controller to set the selected attribute true

    foreach(var item in studentList)
                {
                    if (item.Value == appoinmnetRec.FK_StudentId.ToString())
                    {
                        item.Selected = true;
                    }
                }
    

None of the above methods working. Please help, thanks in advance

Upvotes: 0

Views: 113

Answers (1)

Hien Nguyen
Hien Nguyen

Reputation: 18975

I tried to reproduce your issue. In my machine, selected value worked.

Controller:

namespace WebApplication2.Controllers
{
    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            ViewBag.Title = "Home Page";
            var studentList = new List<SelectListItem>()
            {
                new SelectListItem {Text = "ABC", Value = "1"},
                new SelectListItem {Text = "CDE", Value = "2"},
            };
            ViewData["studentList"] = studentList;
            return View();
        }

        public ActionResult Student()
        {
            var studentList = new List<SelectListItem>()
            {
                new SelectListItem {Text = "Peter Cech", Value = "S001"},
                new SelectListItem {Text = "Leo Messi", Value = "S002"},
            };
            ViewData["studentList"] = studentList;
            AppointmentViewModel model = new AppointmentViewModel();
            model.FK_StudentId = "S001";

            return View(model);
        }
    }

    public class AppointmentViewModel
    {
        public string FK_StudentId { get; set; }
    }
}

View: Student.cshtml

@model WebApplication2.Controllers.AppointmentViewModel

@{
    ViewBag.Title = "Student";
}

<h2>Student</h2>

<script src="~/Scripts/jquery-1.10.2.js"></script>

@Html.DropDownList("StudentNo", ViewData["studentList"] as List<SelectListItem>, "---Please Select---", new { @class = "form-control selectpicker", id = "studentIdDrp" })

<script>
    $(document).ready(function () {

        $("#studentIdDrp").val('@Model.FK_StudentId');
    });
</script>

Upvotes: 1

Related Questions