第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

使用 SelectListItem 設(shè)置 DropDownList 的選定值

使用 SelectListItem 設(shè)置 DropDownList 的選定值

C#
躍然一笑 2022-11-21 16:50:11
雖然這個(gè)問題已經(jīng)被問過好幾次了,但我仍然在努力尋找解決問題的方法我有一個(gè)下拉列表,我想在檢索數(shù)據(jù)時(shí)綁定選定的值。這是我的控制器studentList = db.Students                    .Select(x => new SelectListItem                    {                        Value = x.StudentId.ToString(),                        Text = x.StudentNo + " - " + x.StudentNameEn                    }).ToList();                ViewData["studentList"] = studentList;這是我的觀點(diǎn) @Html.DropDownList("StudentNo", ViewData["studentList"] as List<SelectListItem>, "---Please Select---", new { @class = "form-control selectpicker", id = "studentIdDrp" })我試過的我嘗試使用 jquery 綁定值$("#studentIdDrp").val('@Model.AppointmentViewModel.FK_StudentId');我嘗試從控制器中將所選屬性設(shè)置為 trueforeach(var item in studentList)            {                if (item.Value == appoinmnetRec.FK_StudentId.ToString())                {                    item.Selected = true;                }            }以上方法均無效。請幫助,提前致謝
查看完整描述

1 回答

?
白衣非少年

TA貢獻(xiàn)1155條經(jīng)驗(yàn) 獲得超0個(gè)贊

我試圖重現(xiàn)你的問題。在我的機(jī)器中,選擇的值有效。


控制器:


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>


查看完整回答
反對 回復(fù) 2022-11-21
  • 1 回答
  • 0 關(guān)注
  • 170 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號