【问题标题】:Set the selected value for a DropDownList using SelectListItem使用 SelectListItem 设置 DropDownList 的选定值
【发布时间】:2019-03-31 12:51:31
【问题描述】:

虽然这个问题已经被问了好几次了,但我仍然在努力寻找一个问题的解决方案

我有一个下拉菜单,我想在检索数据时绑定选定的值。 这是我的控制器

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

这是我的看法

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

我的尝试

  1. 我尝试使用 jquery 绑定值

    $("#studentIdDrp").val('@Model.AppointmentViewModel.FK_StudentId');
    
  2. 我尝试从控制器设置选定的属性为真

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

以上方法均无效。请帮忙,提前谢谢

【问题讨论】:

  • $("#studentIdDrp").val('@Model.AppointmentViewModel.FK_StudentId') 在哪里,是在 cshtml 文件中还是在另一个 .js 文件中?
  • html页面内script标签下

标签: c# jquery asp.net-mvc model-view-controller


【解决方案1】:

我试图重现您的问题。在我的机器上,选择的值有效。

控制器:

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

查看: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>

【讨论】:

  • 感谢您的支持,由于 selectpicker 类,我的代码无法正常工作,现在我修复了它。
猜你喜欢
  • 2011-11-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-10-30
  • 1970-01-01
  • 1970-01-01
  • 2015-06-14
  • 2013-04-24
相关资源
最近更新 更多