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>
- 1 回答
- 0 關(guān)注
- 170 瀏覽
添加回答
舉報(bào)